将根目录重定向到具有 .htaccess 的子文件夹,但避免与动态 URL 冲突



为了清楚起见,这是我的所有规则

<IfModule mod_rewrite.c>
      Options +FollowSymlinks
    # Options +SymLinksIfOwnerMatch
      RewriteEngine On
    # RewriteBase /
    # Adaptive-Images -----------------------------------------------------------------------------------
    # Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
    # RewriteCond %{REQUEST_URI} !ignore-this-directory
    # RewriteCond %{REQUEST_URI} !and-ignore-this-directory-too
      RewriteCond %{REQUEST_URI} !cache
      RewriteCond %{REQUEST_URI} !img
      RewriteCond %{REQUEST_URI} !ui
    # don't apply the AI behaviour to images inside AI's cache folder:
      RewriteCond %{REQUEST_URI} !ai-cache    
    # Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
    # to adaptive-images.php so we can select appropriately sized versions
      RewriteRule .(?:jpe?g|gif|png)$ /adaptive-images.php
    # END Adaptive-Images -------------------------------------------------------------------------------
    # Add a trailing slash to folders that don't have one
      RewriteCond %{REQUEST_URI} !(/$|.)
      RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
    # Exclude these folders from rewrite process
      RewriteRule ^(admin|ajax|img|inc|js|lang|lib|pub|tpl|ui)($|/)   -   [L]
    # Redirect root requests to /home/ folder
      RewriteRule ^(/home/)?$             /home/                                  [NC,L]
    # Start rewriting rules
      RewriteRule ^(.*)/(.*)/(.*).htm$    /home/details.php?rCat=$1&kParam=$3    [NC,L]
      RewriteRule ^(.*)/(.*)/$            /home/master.php?rCat=$1&rCity=$2       [NC,L,QSA]
      RewriteRule ^(.*)/$                 /home/master.php?rCat=$1                [NC,L,QSA]
      RewriteRule ^(.*).htm$              /home/page.php?rPage=$1                 [NC,L]
</IfModule>

我想要:

  1. 键入www.mysite.com将重定向到www.mysite.com/home/目录(无需在地址栏上显示/home/部分(
  2. 键入www.mysite.com/home(带或不带尾部斜杠(保留在目录中www.mysite.com/home/;上面的列表中有一条规则,当目录没有斜杠时,会添加尾部斜杠
  3. 键入(或单击类似链接(www.mysite.com/my-category重定向到动态网址,例如/home/master.php?rCat=my-category

如您所见,/home/是唯一的"真实"目录。目前,我怀疑尽管有[L]RewriteRule ^(.*)/$还是覆盖了RewriteRule ^(/home/)?$:当我输入www.mysite.com似乎我被重定向到www.mysite.com/home(这很好(,但随后这个 URL 被"翻译"成/home/master.php?rCat=home:由于"主页"类别不存在,我得到一个空的类别页面模板。

那么,我怎么能告诉.htaccess"请不要应用于规则RewriteRule ^(.*)/$的/home/路径,但请停止RewriteRule ^(/home/)?$"?

这样说:

<IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
      RewriteRule ^/?$ /home/ [L]
      # Add a trailing slash to folders that don't have one
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_URI} !(/$|.)
      RewriteRule . %{REQUEST_URI}/ [R=301,L]
      RewriteCond %{REQUEST_URI} !(cache|img|ui)
      RewriteRule .(?:jpe?g|gif|png)$ /adaptive-images.php [L,NC]
      # skip all files and directories from rewrite rules below
      RewriteCond %{REQUEST_FILENAME} -d [OR]
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteRule ^ - [L]
      # Exclude these folders from rewrite process
      RewriteRule ^(admin|ajax|img|inc|js|lang|lib|pub|tpl|ui)($|/) - [L]
      RewriteRule ^([^/]+)/([^/]+)/(.+?).htm$ /home/details.php?rCat=$1&kParam=$3 [NC,L,QSA]
      RewriteRule ^([^/]+)/([^/]+)/$ /home/master.php?rCat=$1&rCity=$2 [NC,L,QSA]
      RewriteRule ^(.+?).htm$ /home/page.php?rPage=$1 [NC,L,QSA]
      RewriteRule ^([^/]+)/$ /home/master.php?rCat=$1 [NC,L,QSA]
</IfModule>

最新更新