Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 314
youeye Avatar asked Nov 28 '22 15:11

youeye


2 Answers

Redirect One URL to Another URL by Using htaccess file:

Redirect 301 /en/php/project.html http://www.example.org/newpage.html
like image 146
Hire CMS Expert Avatar answered Dec 04 '22 09:12

Hire CMS Expert


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.

like image 21
Sumurai8 Avatar answered Dec 04 '22 10:12

Sumurai8