简体   繁体   中英

.htaccess rewriting with GET parameter

I want to set up .htaccess file for rewrting url like example.com/33 to example.com?id=33 (example.com/index.php?id=33) and example.com/Test to example.com?name=Test (example.com/index.php?name=Test)

My htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ /index.php?id=$1 [QSA,L]

But when I goto url example.com/33 it will redirect me to index.php, but id parameter doesn't exist. Where can be the problem please?

Try this

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?id=$1 [L]

Have 2 rewrite rules, one for digits (id) and another for name parameter:

RewriteEngine On

# ignore all existing files and directories for rewriting     
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(\d+)/?$ index.php?id=$1 [QSA,L]

RewriteRule ^(\w+)/?$ index.php?name=$1 [QSA,L]

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