如何使用 mod_rewrite 从我的 URL 中删除查询字符串?
我是 mod_rewrite 的新手,如果可能的话需要一些帮助。
这是我的旧网址,
www.mysite.com/web/page.php?c=categoryname
我需要更改它:
www.mysite.com/web/page/categoryname.html
我正在使用此代码,但它不起作用:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
如何修复上述代码以正确重写我的网址? 编辑:
感谢乌尔里希·帕尔哈 我设法重定向 html 文件,但 css、js 等没有重定向,而且网站看起来很糟糕,这是我现在使用的代码,直到我找到一个也能重定向 css 的人,
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
请问有什么帮助吗?
I am new at mod_rewrite and need some help if is possible.
this is my old url
www.mysite.com/web/page.php?c=categoryname
I need to change it at:
www.mysite.com/web/page/categoryname.html
I am using this code but it doesn't work:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
How can I fix the above code to correctly rewrite my URLs?
EDIT:
Thanks to Ulrich Palha
i manage to redirect the html files, but css,js etc are not redirect and the website looks rly bad, this is the code i am using now, till i will get the one who will redirect css too
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
BUMP ANY HELP PLEASE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将以下内容添加到站点 Web 目录中的
.htaccess
文件中,替换上面的规则。Add the following to the
.htaccess
file in the web directory of your site, replacing the rules you have above.规则中的正则表达式是错误的 - 您缺少
/page
,添加了不应该的可选最后斜杠,并且忘记删除.html
部分。此外,您的RewriteBase
不应该有最后的斜杠,并且您的规则也不应该有前导斜杠。总而言之,应该是这样的:
Your regex in the rule is wrong — you are missing
/page
, adding an optional final slash that you shouldn't be, and forgetting to remove the.html
part. Also yourRewriteBase
shouldn't have a final slash and your rule shouldn't have a leading slash.It total, it should be this: