如何将所有页面请求重定向到一个页面而不更改URL

时间:2015-09-16 17:17:05

标签: php apache .htaccess mod-rewrite redirect

我想将我网站上的所有网页(包括索引)重定向到UnderWork.html 我使用.htaccess使用以下代码执行此操作:

RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html

......它的工作正常。 现在我向我的.htaccess添加更多代码,将所有流量重定向到非www域,现在我的代码如下所示:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html

这是我遇到问题的地方。如果我输入类似:domain.in/xyz的网址,那么它会像以前一样工作,但如果我使用类似:www.domain.in/xyz的网址,那么Apache会将其转换为coincart.inxyz/

我在这里做错了什么?我怎样才能得到我想要的东西?提前致谢。

1 个答案:

答案 0 :(得分:2)

以下规则对我有用:

RewriteEngine On

# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]

# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]

# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]

要在请求目录根目录(特别是域根目录)时显示自定义页面,请使用Apache的DirectoryIndex指令:

DirectoryIndex UnderWork.html
相关问题