【发布时间】: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