0

我有以下代码,我正在尝试做类似的事情。假设我有一个地址 - mydomain.com/server2/ 当我在斜杠后键入任何内容时,例如 mydomain.com/server2/whatever 我想加载 server2.php 但地址必须相同

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteRule ^index\.php/?$ / [L,R=301,NC]

    # Hide File Extensions
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^/]+)/$ $1.php

    # Add 301 redirects to new extensionless file
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]


</IfModule>

我也不知道上面的代码是否 100% 正确。谢谢你的帮助。

4

1 回答 1

1

您的代码中存在问题,并且在某些 URL 中它会给您无限循环。用这个替换你的代码:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1/ [R=301,L,NE]

    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

</IfModule>
于 2013-11-10T14:09:05.397 回答