【问题标题】:How to write an exception for a specific folder如何为特定文件夹编写异常
【发布时间】:2023-03-21 16:02:02
【问题描述】:

我的任务是修改 .htaccess 文件中对移动网站的重定向,以仅重定向主页。问题是主文件夹包含许多属于它们自己的页面的子文件夹,它们错误地重定向到主站点移动版本。

这是代码:

 RewriteOptions inherit
<IfModule mod_expires.c>
ExpiresActive On
#ExpiresDefault "access plus 10 second"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
# Webfonts
ExpiresByType application/vnd.ms-fontobject         "access plus 1 year"
ExpiresByType font/eot                              "access plus 1 year"
ExpiresByType font/opentype                         "access plus 1 year"
ExpiresByType application/x-font-ttf                "access plus 1 year"
ExpiresByType application/font-woff                 "access plus 1 year"
ExpiresByType application/x-font-woff               "access plus 1 year"
ExpiresByType font/woff                             "access plus 1 year"
ExpiresByType application/font-woff2                "access plus 1 year"
</IfModule>

<filesMatch ".(js)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

<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>

#### MOBILE REDIRECTION START

RewriteBase /

# Check if m=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if m=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device

# Check if we're not already on the mobile site
# Check to make sure we haven't set the cookie before
# Now redirect to the mobile site
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$
RewriteCond %{HTTP_HOST}          !^m\.
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ http://m.siteurl.com [R,L]

#### MOBILE REDIRECTION END

有人可以帮我在哪里以及如何放置除主要文件夹(站点)之外的文件夹(站点)的例外情况。

【问题讨论】:

    标签: html .htaccess redirect


    【解决方案1】:
    RewriteRule ^ http://m.siteurl.com [R,L]
    

    最后一个RewriteRule 指令将所有内容(与正则表达式^ 匹配)重定向到移动网站的根目录(尽管您完全错过了目标URL 上的尾部斜杠,这浏览器需要为您附加。)

    要“仅重定向主页”,您需要更具体地使用正则表达式并仅匹配空的 URL 路径。例如。 ^$。例如:

    RewriteRule ^$ http://m.siteurl.com/ [R,L]
    

    但是,用户可能会在任何页面进入网站,所以您不想将任何 URL 重定向到移动网站上相应的 URL-path 路径吗?

    例如,要将/&lt;anything&gt; 重定向到http://m.siteurl.com/&lt;anything&gt;,您需要使用RewriteRule 模式 捕获URL 路径。例如:

    RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
    

    其中$1 是对捕获的 URL 路径(即(.*))的反向引用。


    如何为特定文件夹编写异常

    您问题的标题似乎与您在问题主体中提出的问题相冲突,即。仅重定向主页。

    但是,要回答这个问题...要创建“特定文件夹的例外”,您需要在上述 RewriteRule 指令之前添加一个额外的 RewriteCond 指令,以检查请求是否未映射到“特定文件夹”文件夹”。

    例如:

    RewriteCond %{REQUEST_URI} !^/specific-folder/
    RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
    

    仅当请求的 URL ! 前缀)以/specific-folder/ 开头时,才会发生以下RewriteRule 替换


    旁白:

    • 您的移动网站没有使用 HTTPS 吗?

    • RewriteOptions inherit - 你“继承”了哪些指令?

    【讨论】:

      【解决方案2】:

      感谢您的回答。我会再澄清一点。 我在主机上创建了一个新站点,其中包含许多其他(更老的)站点。当我试图从我的手机进入该站点时,我意识到它被重定向到该“主站点”的移动站点。 我查看了 public_html 文件夹并意识到很久以前有人在 public_html 文件夹中创建了一个站点,并在 .htaccess 中进行了重定向,然后其他人创建了许多与其他站点的子文件夹,包括我创建的站点的子文件夹和上传到他们的服务器。 现在的问题是子文件夹中的那些站点被重定向到在主 public_html 文件夹中创建的站点的移动站点。

      回答“旁白”部分 - 这不是我的网站,我相信它也不会t. It is a very old site that needs a complete rehaul. Rewrite options inherit - thats 我的问题。我m not sure, as Im 不是创作者。这完全是一团糟,但现在我只需要解决重定向问题。

      我希望能澄清一点。

      编辑: 似乎删除Rewrite options inherit 可以解决问题。现在只有主站点被重定向到移动版本。

      【讨论】:

        猜你喜欢
        • 2015-02-01
        • 2011-12-19
        • 1970-01-01
        • 2014-06-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多