【问题标题】:Need a specific URL RewriteRule in .htaccess在 .htaccess 中需要一个特定的 URL RewriteRule
【发布时间】:2015-02-13 13:39:43
【问题描述】:

我是设置 .htaccess 的新手,希望能在我的问题上提供一些帮助。

如果用户点击以下网址(原始网址):

http://www.example.com/somepage/something

我希望它重定向到:

http://www.example.com/somepage

并在浏览器中保留原始 URL。

到目前为止,我有以下内容:

RewriteRule ^somepage/(.*)$ /somepage [R=301]

但是,它不起作用。

我如何让它工作?

编辑:

这是我的 .htaccess 文件现在的样子:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

RewriteRule ^somepage/(.*)$ /somepage [R=301]

【问题讨论】:

  • Rahul,您不能重定向在浏览器中保持相同的 URL。如果您有处理对/somepage 的请求的规则,那么您可以简单地删除R=301。还建议将其替换为L,以表明如果找到匹配项,它将不会继续检查规则。 编辑:也许您想分享您的 .htaccess 文件,以便我们可以查看上下文中的所有内容?
  • @MikeAnthony 我在上面的问题中添加了 .htaccess 文件。我在 SO 上问过这个问题,因为我的朋友建议在幕后显示另一个 URL 时显示一个 URL,但他不记得那个 RewriteRule。

标签: regex apache .htaccess mod-rewrite rewrite


【解决方案1】:

这个规则是有问题的:

RewriteRule ^somepage/(.*)$ /somepage [R=301]

有两个原因:

  1. 由于您不想更改 URL,因此您不能在此规则中使用 R=301 标志。
  2. 更大的问题是您的正则表达式^somepage/(.*)$ 也将匹配/somepage/,而您需要匹配/somepage/something

要解决此问题,请使用完整的 .htaccess,如下所示:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
RewriteBase /

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ [NC]
RewriteRule ^(.+?)\.html$ /$1 [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
RewriteRule ^([^/.]+) $1.html [L]

【讨论】:

  • 抱歉,这没用。当我点击原始 URL 时,我收到了 500 Internal Server Error。在我的项目中,somepage 实际上是cord-blood-basics,所以我在正则表达式中尝试了cord-blood-basicscord\-blood\-basics,但这也没有用。一开始我也试过没有^,但也没有用。
  • somepage 不是一个真正的目录,它是一个 HTML 文件。 something 是这个 HTML 页面的手风琴中的按钮,当这个 URL 被点击时,我试图以编程方式单击它。我知道这对您来说可能听起来很奇怪,但如果您参考 this question that I posted earlier today,您会更好地了解上下文。而且,这是我项目中唯一的 .htaccess 文件。
  • xampp\apache\logs\error.log,这是我在点击该网址后得到的:[Fri Feb 13 21:25:18.260038 2015] [core:error] [pid 6176:tid 1692] [client 127.0.0.1:55869] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
  • 现在它给了我一个 404 错误。当我查看开发工具的网络选项卡时,我看到它正在寻找 HTML 类型的文件 something。 Apache 错误日志中没有错误。
  • 我的大部分回答也包含在您的问题中。如果您的问题是关于RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC],那么它正在检查.html 文件的存在。 %{DOCUMENT_ROOT} 是您的 Web 根目录的 Apache 属性,$1 是从 RewriteRule 中的匹配组 #1 中捕获的值。
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2017-08-13
  • 2012-07-21
  • 1970-01-01
相关资源
最近更新 更多