简体   繁体   English

使用.htaccess将一个网址重定向到另一个网址

[英]Redirect one url to another url using .htaccess

As i am trying to redirect one complete URL 因为我试图重定向一个完整的URL

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

To this URL 到这个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". 但这不起作用,而是说我要求的URL说“禁止”。

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. RewriteRule不包含查询字符串,也不包括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) . 如果没有http主机,您将匹配download/page=download(etc)downloadpage=download(etc)

You'll need 2 rules. 你需要2条规则。 One that redirects the ugly url to the nice url. 一个将丑陋的URL重定向到漂亮的URL。 One rule needs to rewrite the nice url to an actual working url: 一条规则需要将好的URL重写为实际的工作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. 第二个规则使用QSA标志,这意味着它将原始查询字符串附加到重写中的查询字符串。 The END flag stops all rewriting, as the L-flag doesn't do that in .htaccess context. END标志停止所有重写,因为L-flag不会在.htaccess上下文中执行此操作。 END is only available from Apache 2.3.9 onwards and will cause an internal server error if used in an version before that. END仅从Apache 2.3.9开始提供,如果在此之前的版本中使用,将导致内部服务器错误。 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. 编辑:请注意,您永远不应使用PERMANENT重定向测试htaccess规则。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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