使用常量querystring重写所有.php页面的规则
我正在处理一个项目,其中所有页面都需要假定查询字符串id。例如,以下页面:使用常量querystring重写所有.php页面的规则,php,.htaccess,mod-rewrite,url-rewriting,rewrite,Php,.htaccess,Mod Rewrite,Url Rewriting,Rewrite,我正在处理一个项目,其中所有页面都需要假定查询字符串id。例如,以下页面: example.com/alfa.php?id=1 example.com/beta.php?id=30 exapmle.com/gamma.php?id=50 我正在使用以下.htaccess使我的URL变得漂亮 RewriteEngine On ## BEAUTIFUL URL DirectoryIndex default.php Options -Multiviews -Indexes +FollowSymLink
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
此代码将进行以下转换:
example.com/alfa.php?id=1到example.com/alfa?id=1
假设X.php每次都在变化(X),但id始终不变,那么.htaccess文件的最佳内容是什么
我试过几种方法,但最好的是什么
还有一件事,我怎样才能将URL转换为
example.com/alfa/1
和example.com/beta/30
您试图在规则的regex模式中匹配查询字符串(所有之后的内容?
)。查询字符串不是该匹配的一部分,只是URI。但是,由于您不希望触碰查询字符串,因此不要与之匹配,因为它会自动追加:
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/$ $1 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w/-]+)$ $1.php [L,T=application/x-httpd-php]
对于example.com/alfa/1
和example.com/beta/30
:
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /([\w/-]+)(\.php)?\?id=([0-9]+)
RewriteRule ^ /%1/%3 [L,R=301]
RewriteRule ^([\w/-]+?)/([0-9]+)$ $1.php?id=$2 [L,QSA,T=application/x-httpd-php]
你可以尝试做的另一件事是摆脱你的规则,只使用你已经关闭的
多视图。多视图和mod_协商就是为这样的东西而做的。谢谢。但是如果我想把它转换成example.com/alfa/1
亲爱的Jon Lin,我试着使用你的第二个代码。但是当我尝试example.com/alfa/1