简体   繁体   English

htaccess网址重写和重定向不起作用

[英]htaccess url rewrites and redirects not working

I have a website that was updated. 我有一个已更新的网站。 I want to make the old urls work, I want to redirect some of them, so links from google search still work, and rewrite the url for other urls, using .htaccess. 我想使旧的url有效,我想重定向其中一些,因此来自Google搜索的链接仍然有效,并使用.htaccess重写其他url的URL。

For example: 例如:

  • one rewrite is a.com/services to a.com/consultancy 一种重写是a.com/services到a.com/consultancy
  • one redirect is a.com/services/a/b to a.com/consultancy 一种重定向是将a.com/services/a/b重定向到a.com/consultancy

One extra comlpication: the site sends every request to /index.php because the URLs are not to physical files, but the index.php script does internal routing, using the requested path, to serve the right content. 另一个值得一提的是:该站点将每个请求发送到/index.php,因为这些URL并非针对物理文件,但是index.php脚本使用所请求的路径进行内部路由以提供正确的内容。

I'm not sure if I can do a rewrite (services->consultancy) an then another rewrite (consultancy->index.php) in the same htaccess. 我不确定是否可以在同一htaccess中进行重写(服务->咨询),然后再进行重写(consultancy-> index.php)。

Trying this, I'm getting an internal server error 500 for any URL: 尝试此操作,对于任何URL,我都收到内部服务器错误500:

RewriteEngine On

#NEW
RewriteCond %{REQUEST_URI} ^services$ [NC]
RewriteRule ^services$ consultancy [L]

#LEGACY
RewriteRule (.*) ./index.php

Also tried the Redirect 301 directive but had no luck, the same error 500. 还尝试了Redirect 301指令,但是没有运气,同样的错误500。

Any ideas of how to mix rewrites, redirects and the final rewrite to index.php? 关于如何混合重写,重定向和最终重写到index.php的任何想法?

Thanks! 谢谢!

Update 更新

For the combination of redirect and rewrite I realized some of the rules are a little different than my original question, and those might be causing problems, here is an example: 对于重定向和重写的组合,我意识到某些规则与我原来的问题有些不同,并且可能会引起问题,下面是一个示例:

# Redirect to show "consultancy" in the URL
RewriteRule ^services/a/b?$ consultancy [QSA,R,L,NC]

# If "consultancy" is in the URL, change it to the real path
# "consultancy" is an alias of the path that should be processed by index.php
RewriteRule ^consultancy/?$ en/consultoria [NC]

# Should get the "en/consultoria" path
RewriteRule ^ ./index.php [L]

The problem with the previous example is that I get the redirect "services/a/b" -> "consultany" OK, but the rewrite "consultancy" -> "en/consultoria" is not done. 上一个示例的问题在于,我获得了重定向“ services / a / b”->“ consultany”,但未完成重写“ consultancy”->“ en / consultoria”的操作。

Any ideas? 有任何想法吗?

You can use: 您可以使用:

RewriteEngine On

#rewrite
RewriteRule ^services/?$ consultancy [L,NC]

#redirect
RewriteRule ^services/a/b/?$ consultancy [L,NC,R=302]

#LEGACY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

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

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