简体   繁体   中英

mod_rewrite - passing a variable directly from home url not working

I already created a rule that allows users to pass a variable like this: somepage.com/database/12345 which internally changes it to somepage.com/database/?db_index=12345 and it works fine. The code looks like this:

RewriteBase /
RewriteRule ^database/([0-9]+)/?$ /database/?db_index=$1

Now I need to do exactly the same but without the need to pass "database/" before the variable. So the users can pass the variable like this: somepage.com/12345 . It has to be an additional option, not a replacement for the previous one. I tried to do this like that:

RewriteRule ^([0-9]+)/?$ /database/?db_index=$1 [L]

but I just can't get this to work and I have no idea why. The additional code above is inserted directly before the previous working rule.

I'll be really happy for any help and explanation why my method doesn't work.

UPDATE: the whole .htaccess file looks like this:

# Block the include-only files.
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]

# direct url rewrite
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/?$ /database/?db_index=$1 [L]
RewriteRule ^database/([0-9]+)/?$ /database/?db_index=$1

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

您的样子对我来说很好,但是尝试: RewriteRule ^/?([0-9]+)/?$ /database/?db_index=$1 [R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM