1

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

2 Answers 2

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 .

Sign up to request clarification or add additional context in comments.

7 Comments

I tried this, but nothing changed. Along with your answer, I tried all answer provided in stackoverflow, but no luck. This one RewriteCond %{QUERY_STRING} . RewriteRule ^ %{REQUEST_URI}? [R=301,L] changed http://localhost/ourallnews/category.php?cat=News to http://localhost/ourallnews/category.php But can't fulfill my requirements. Please see updated question. Thanks for your co-operation.
@AnandHmt What url are you going to and What error do you get?
Is there any way to show htaccess error log ? When adding this line in htaccess, nothing change. I mean when http://localhost/ourallnews/category.php?cat=News link is visited, then it would be automatically changed in http://localhost/ourallnews/news/. but it doesn't changed, still same url with querystring remained.
This rule is working fine for me on my server. I think you have placed it somewhere in your htaccess where it's not being called or being overridden by other rules. Put this at the top of your htaccess (before other directives) and clear your browser cache to test it.
I added top, cleared cache, delete and recreate .htaccess adding only this 3 lines, still no luck, Does html/php coding structure affect htaccess rule ? I think the problem is from php/html file
|
0

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

1 Comment

I tried this, but nothing changed. Along with your answer, I tried all answer provided in stackoverflow, but no luck. This one RewriteCond %{QUERY_STRING} . RewriteRule ^ %{REQUEST_URI}? [R=301,L] changed http://localhost/ourallnews/category.php?cat=News to http://localhost/ourallnews/category.php But can't fulfill my requirements. Please see updated question. Thanks for your co-operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.