我是设置.htaccess的新手,我希望在我的问题上有所帮助。
如果用户点击以下URL(原始URL):
http://www.example.com/somepage/something
我希望它redirect到:
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} ^[AZ]{3,9}\ /.*\.html\ HTTP/ RewriteRule ^(.*)\.html$ /$1 [R=301,L] RewriteRule ^somepage/(.*)$ /somepage [R=301]
这个规则是问题:
RewriteRule ^somepage/(.*)$ /somepage [R=301]
有两个原因:
R=301
标志。 ^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} ^[AZ]{3,9}\ /.*\.html\ HTTP/ [NC] RewriteRule ^(.+?)\.html$ /$1 [R=301,L] RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] RewriteRule ^([^/.]+) $1.html [L]