url construction htaccess

扶醉桌前 提交于 2019-12-12 02:17:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!