问题
Lets say I have a link to one of my page that looks like: mysite.com/48YSWD96, I need it to look like: mysite.com/?d=48YSWD96. How do I achieve this? Can I achieve this by modifying my htaccess file? which currently looks like this...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteBase /
回答1:
It looks like you just need to append this to the end:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)$ /?id=$1 [L]
For it to work the other way around:
RewriteCond %{QUERY_STRING} (^|&)id=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /%1 [L]
This will take a request for mysite.com/?d=48YSWD96
and change the URI to /48YSWD96
. Essentially, whatever the id equals in the query string.
来源:https://stackoverflow.com/questions/8454122/url-construction-htaccess