简体   繁体   中英

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.

使用htaccess文件将一个URL重定向到另一个URL:

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

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.

你可以使用这个规则

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM