如何使用 mod_rewrite 从我的 URL 中删除查询字符串?

发布于 2025-01-01 03:41:12 字数 1124 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2025-01-08 03:41:12

将以下内容添加到站点 Web 目录中的 .htaccess 文件中,替换上面的规则。

RewriteEngine On
RewriteBase /web/

#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC] 
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#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]

Add the following to the .htaccess file in the web directory of your site, replacing the rules you have above.

RewriteEngine On
RewriteBase /web/

#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC] 
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#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]
善良天后 2025-01-08 03:41:12

规则中的正则表达式是错误的 - 您缺少 /page,添加了不应该的可选最后斜杠,并且忘记删除 .html 部分。此外,您的 RewriteBase 不应该有最后的斜杠,并且您的规则也不应该有前导斜杠。

总而言之,应该是这样的:

RewriteEngine on
RewriteBase /web  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]

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 your RewriteBase shouldn't have a final slash and your rule shouldn't have a leading slash.

It total, it should be this:

RewriteEngine on
RewriteBase /web  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文