问题
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 withourallnews^ourallnews/$which won't match because$means end of string without acategory.php
RewriteRule rewrite:
/ourallnews/%1/?is your desired output URL- There's no need to map to
category.phpin the rewrite result
来源:https://stackoverflow.com/questions/50559638/cannot-redirect-querystring-url