【问题标题】:301 Redirect via .htaccess isn't working301 通过 .htaccess 重定向不起作用
【发布时间】:2017-03-04 15:01:57
【问题描述】:

我有大约 50 个旧链接将被重定向到新链接。但是,我没有重定向,而是转到 404 页面。

这是我下面的 htaccess 文件。知道我做错了什么吗?

## Mod_rewrite in use.

RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed 
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.

## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects

Redirect 301 http://domain.me/blog.php?id=7 http://domain.me/blog
Redirect 301 http://domain.me/iiblg/17-0-The-girl-with.html http://domain.me/blog/the-girl-with
Redirect 301 http://domain.me/iiblg/20-0-Sendra-Lake.html http://domain.me/blog/sendra-lake

还有更多链接。我刚刚从这里删除了它。

【问题讨论】:

  • 你启用了 .htaccess 吗??
  • 是的,我确实启用了它。

标签: php .htaccess redirect http-status-code-301


【解决方案1】:

您不能使用重定向指令匹配查询字符串。你需要使用 mod-rewrite 来做到这一点。

要将 example.com/blog.php?id=7 重定向到 example.com/blog,您需要以下规则:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^id=7$
RewriteRule ^blog\.php$ http://example.com/blog/? [NC,L,R]

【讨论】:

  • 谢谢,但其他链接会是什么样子?其他 2 个链接,因为这是其余 URL 的格式。
  • 对于没有querystring的链接,可以使用Redirect指令(旧路径不能以host开头),Redirect 301 /dir/filename.html example.com/newfilename
  • 谢谢。让它完美运行。不知道主机名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2017-06-30
  • 2014-07-05
  • 2014-12-21
  • 2011-03-08
相关资源
最近更新 更多