繁体   English   中英

.htaccess将所有页面重定向到主页,除了少数页面

[英].htaccess redirect all pages to homepage except few of them

我有一个仅保留2个子页面的网站,我想将所有其他子页面重定向到主页。 这是我当前的htaccess代码:

# this is to disable varnish caching on my server
Header set Set-Cookie "_asomcnc=1;   max-age=900; path=/;"

RewriteEngine On

# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# redirect index.html to root
RewriteBase /
RewriteRule ^index\.html$ / [NC,R,L]

# remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]

我尝试了一些在堆栈溢出时在这里找到的示例,但是我不断收到重定向循环,或者未加载CSS文件和图像。 所以可以说我想从这两个文件夹中加载所有文件:

  • / CSS
  • /图片

我也想保留2个子页面:

  • /subpage1.html
  • /subpage2.html

其他所有内容都应重定向到首页。

要排除页面被重写的情况,可以将RewriteCond%{REQUEST_URI}一起使用,也可以将规则链短路。

要提前退出,请在开头插入这些规则

RewriteRule ^css - [L]
RewriteRule ^images - [L]
RewriteRule ^subpage1.html$ - [L]
RewriteRule ^subpage2.html$ - [L]

然后,将其他所有内容重定向到主页,当然,除非已经是主页

RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,L]

RewriteCond做同样的事情

RewriteCond %{REQUEST_URI} !^/css
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{REQUEST_URI} !^/subpage1.html$
RewriteCond %{REQUEST_URI} !^/subpage2.html$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,L]

这将重定向到主页,但前提是REQUEST_URI不是css,不是图像,不是subpage1.html,不是subpage2.html且还不是主页。


当一切正常运行时,可以将R替换为R=301 切勿使用R=301测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM