cannot redirect querystring url

一世执手 提交于 2020-01-05 05:35:07

问题


I am trying to clean url redirecting query string URL. I've tried all answer and rules provided in stackoverflow and in other sites, but nothing changed. I thought it was .htaccess file error, but I found it is working fine to generate other error. I am trying to redirect

http://localhost/ourallnews/category.php?cat=News

To

http://localhost/ourallnews/News/

First I tried using this.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^ourallnews/category\.php$ /ourallnews/%1? [R=302,L]

After that I tried

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)$
RewriteRule ^ourallnews.*$ /ourallnews/%1/? [R=302,L]

Nothing changed.

I clear browser cache, restart apache but doesn't work. Querystring url is remained.

Then finally this one brings change.

RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

It change

http://localhost/ourallnews/category.php?cat=News

to

http://localhost/ourallnews/category.php

But my requirement is show querystring value not URI.

http://localhost/ourallnews/news/

Please help me


回答1:


You can use this

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^ourallnews/category\.php$ /ourallnews/%1? [R=302,L]

This will redirect /ourallnews/category.php?cat=value to /ourallnews/value .




回答2:


The following works for me:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)$
RewriteRule ^ourallnews.*$ /ourallnews/%1/? [R=302,L]

Some differences with yours:

RewriteRule match:

  • ^ourallnews.*$ which will match anything that starts with ourallnews
  • ^ourallnews/$ which won't match because $ means end of string without a category.php

RewriteRule rewrite:

  • /ourallnews/%1/? is your desired output URL
  • There's no need to map to category.php in the rewrite result


来源:https://stackoverflow.com/questions/50559638/cannot-redirect-querystring-url

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