我正在寻找一个“.htaccess”文件解决方案来重写以“./index.html”和“./index”结尾的请求地址,直到只有站点根目录或子文件夹,例如“http:/ /www.mydomain.com/index[.html]“仅限于”http://www.mydomain.com/“。我已经找到了一些关于“index.html”问题的帮助,但是我很难让解决方案适用于“./index"-only请求。我正在使用以下代码尝试“./index"-only篇评论:
# MOD_REWRITE IN USE
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Redirect "index" page requests to either root or directory
# These first two lines work for "./index.html"...
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
## These three lines don't work, last two are alternates of one another...
## RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index*\ HTTP/
## RewriteRule ^index*$ http://%{HTTP_HOST}/ [R=302,L]
## RewriteRule ^index*$ $1 [R=302,L]
# Hide .html extension - additional information
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
(后面的部分用于隐藏“.html”结尾,如上所述,并且可能是多余的,但我按原样包含整个脚本。)我不知道Apache服务器的版本,但是任何帮助都会非常感谢。感谢。
答案 0 :(得分:1)
我已经设法回答了我自己的问题,在此页面上显示的链接,发布后,结合偏离中心的网络搜索特定定义。以下是两个链接:StackOverflow - Apache .htaccess to redirect index.html to root,...和Media Temple Support/Knowledgebase - Using .htaccess rewrite rules - .htaccess。新代码如下所示:
## MOD_REWRITE in use ##
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
## Redirect index page requests to either root or directory ##
RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/
RewriteRule ^(.*)index.*$ "/$1" [R=301,L]
## Hide .html extension ##
## To externally redirect /dir/foo.html to /dir/foo...
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.html...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
可以通过替换
来修改新的第一次重写/条件以使其看起来更像原始的第一次重写/条件RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.*\ HTTP/
RewriteRule ^index.*$ http://%{HTTP_HOST}/ [R=301,L]
取代
RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/
RewriteRule ^(.*)index.*$ "/$1" [R=301,L]
但我不知道你为什么这样做,因为后者工作得很好,文字较少。也许我会错过一些东西 - 但如果没有破坏,请不要修理它。
我还没有能够确定最初的第一次重写/条件来自哪里,但它确实是为它设计的,尽管我正在寻找一些不同的东西。我希望所有这些胡言乱语都对那些寻求这种解决方案的人有意义。感谢“Jon Lin”和“Bip”的回应以帮助,但是,当搜索和重新搜索的时间出乎意料地回报时,它也很好。现在我的网站应该像其他人一样行事。谢谢你们!
答案 1 :(得分:0)