youeye
youeye

Reputation: 727

Redirect one url to another url using .htaccess

As i am trying to redirect one complete URL

http://www.domain-name.com/download/?page=download

To this URL

http://www.domain-name.com/download/show

For this to work I have added this code

rewriterule http://www.domain.com/download/?page=download(.*)$ http://www.domain.com/download/show$1 [r=301,nc]

But this is not working and instead it says the URL I am requesting says "Forbidden".

Can any one please give any solution for this.

Upvotes: 7

Views: 90389

Answers (4)

Pallavi
Pallavi

Reputation: 101

Use RewriteEngine:

RewriteEngine On
Redirect 301 http://www.domain-name.com/download/?page=download http://www.domain-name.com/download/show

Upvotes: 5

Moin
Moin

Reputation: 51

you can use this rule

Redirect /download/?page=download http://www.domain-name.com/download/show

Upvotes: 5

Hire CMS Expert
Hire CMS Expert

Reputation: 361

Redirect One URL to Another URL by Using htaccess file:

Redirect 301 /en/php/project.html http://www.example.org/newpage.html

Upvotes: 26

Sumurai8
Sumurai8

Reputation: 20737

RewriteRule doesn't include the query string and doesn't include the http-host. Besides that, the first argument is a regex pattern. Without the http host you would be matching either download/page=download(etc) or downloadpage=download(etc).

You'll need 2 rules. One that redirects the ugly url to the nice url. One rule needs to rewrite the nice url to an actual working url:

#Rewrite ugly url to nice url
RewriteCond %{QUERY_STRING} ^page=download&?(.*)$
RewriteRule ^download/?$ download/show?%1 [R,L]

#Now get the nice url to work:
RewriteRule ^download/show/?$ download/?page=download [QSA,END]

The second rule uses the QSA flag, which means it will append the original query string to the query string in the rewriterule. The END flag stops all rewriting, as the L-flag doesn't do that in .htaccess context. END is only available from Apache 2.3.9 onwards and will cause an internal server error if used in an version before that. Please note that you probably have to modify the second rule to point to an actual file.

You can find the documentation here.

Edit: Please note that you should NEVER test htaccess rules with a PERMANENT redirect. If you make a mistake, the browser will remember this mistake! Only convert temporary redirects to permanent redirects if everything is working as you expect it to work.

Upvotes: 8

Related Questions