停止.htaccess规则

时间:2015-09-03 05:28:02

标签: .htaccess mod-rewrite redirect

我正在使用.htaccess规则在一个域上进行战斗已经有一段时间了。我找到了重定向所有不存在的文件和文件夹所需的规则,并在我将.htaccess中的位置更改为顶部后使其工作,以便我的.htaccess现在看起来像这样:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ / [R=301,L]
</IfModule>

#Rewrite for non www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/? [R=301,L]

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} acer|alcatel|amoi|android|asus|avantgo|benq|blackberry|blazer|boxee|bravia|cdm-|ce-html|compal|(core|gom|m|vlcmedia|windows-media)player|dlna|docomo|dreambox|elaine|ericsson|fennec|flycast|foobar2000|google\ wireless\ transcoder|googlebot-mobile|googletv|gt-|hbbtv|hiptop|htc|huawei|iemobile|im-|ipad|ipaq|iphone|ipod|iris|itunes|kindle|large|lg(-|\/|_|1|2|3|4|5|6|7|8|9|e|http)|lenovo|levis|maemo|midp|miro|mmp|mot-|mobile|motorola|msn.*tv|mtv|nettv|nexus|nintendo|nokia|opera\ (mobi|mini)|palm|panasonic|pantech|pg-|philips|pixi\/|playstation|plex|plucker|pocket|portalmmm|pre\/|psp|quicktime|qtek|rim|sagem|samsung|sanyo|sch-|screen|sec-sgh|sendo|sfr|sgh-|sharp|sie-|smart(hub|phone|.?tv)|sonyericsson|sph-|sprint|spv|swisscom|symbian|t-mobile|tablet|toshiba|touch|treo|upnp|up.browser|vertu|videoweb|vk-|vodafone|volksbox|wafa|wap|webos|webtv|wii|windows\ (ce|phone)|wml|wonu|xbmc|xbox|xda|xiino|zte|zune [NC]
RewriteRule ^(.*)$ http://www.domain.tld/m/ [L,R=302]


## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_deflate.c>
    <filesMatch "\.(js|css|html|php|jpg|jpeg)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>

我的问题是我的子文件夹需要自己的重定向规则,用于不存在的文件/文件夹。我不希望将它们重定向到根目录,而是将它们重定向到正确的索引&#39;文件夹&#39;。

网站构建如下:

http://www.domain.tld/ <-- Mainsite
http://www.domain.tld/keywordX/1/ <-- Index folder for keyword X
http://www.domain.tld/keywordY/1/ <-- Index folder for keyword Y
http://www.domain.tld/keywordZ/1/ <-- Index folder for keyword Z

在我添加规则以从root重定向所有不存在的文件/文件夹之后,它也会将子文件夹中的非现有文件/文件夹重定向到根目录。

我之后以我发现排除目录的方式更改了我的.htaccess的第一部分,但结果是重定向错误:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(keywordX|keywordY|keywordZ)($|/) - [L]
  RewriteRule ^(.*)$ / [R=301,L]
</IfModule>

有什么建议我可以避免这种行为吗?

以下我试图在/ keywordX /文件夹中添加htaccess,也在/ keywordX / 1 /文件夹中添加: 我发现添加&#34; RewriteEngine On&#34;会做的伎俩。但它没有。 我发现Line 1 RewriteEngine Off和Line 2 RewriteEngine On会起作用,但它也没有。

更新3:

第一次正确加载网站并且根目录的重定向最多。所有现有文件夹都没有重定向(不知道为什么它不重定向它们,因为它们不被排除),但到目前为止kproxy显示我kategoryX / kategoryY / kategoryZ正在加载它们应该。

看起来我在正确的道路上进行了之前的试用,只是我的文件排除不正确。但是经过越来越多的阅读,慢慢地但越来越生气,我偶然发现文件扩展名被排除在外。

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(images)/ [NC]
RewriteCond %{REQUEST_URI} !\.(xml|txt|css)$
RewriteRule ^.+$ / [R=302,NC,L]

使用这段代码,我能够将所有不正确的FOLDER请求重定向回root。

现在缺少的是重定向不正确的文件(重定向的主要部分)...文件,如123_itemname.html,456_itemname.html等。

有没有办法扩展我现在使用的htaccess代码,还是需要一个全新的方法?

编辑:我已经对问题做了很多编辑,因为我等不及让其他人弄清楚什么是错的。

0 个答案:

没有答案
相关问题