修改 URL 为 .htaccess [#Q1.]
Modify URL with .htaccess [#Q1.]
我想使用 .htaccess
修改服务器上的 URL,但我遇到了一些问题。
结帐.
规则 #1:
https://example.com/site/index.php
https://example.com/site/
should get converted to short url.
https://example.com/
规则 #2:
https://example.com/
should load full url without expanding.
https://example.com/site/index.php
.
规则 #3:
https://example.com/a/site/index.php
https://example.com/b/site/index.php
https://example.com/a/site/
https://example.com/b/site/
should get converted to short url.
https://example.com/a/
https://example.com/b/
注意: 我想从 URL 中删除 site
文件夹,但我不知道写 .htaccess
,我们将不胜感激。
您可以在站点根目录 .htaccess 中尝试这些规则(假设同级目录中没有其他 .htaccess):
RewriteEngine On
# matches /site/ or /site/index.php or
# /a/site/index.php or /b/site/index.php
# captures a/ or b/ or an empty string in %1
# redirects to /a/ or /b/ or /
RewriteCond %{THE_REQUEST} \s/+([ab]/|)site/(?:index\.php)?[?\s] [NC]
RewriteRule ^ /%1 [L,R=302,NE]
# matches a/ or b/ or empty (landing page)
# rewrites to a/site/index.php or b/site/index.php or site/index.php
RewriteRule ^([ab]/)?$ site/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.+?)/?$ .php [L]
我想使用 .htaccess
修改服务器上的 URL,但我遇到了一些问题。
结帐
规则 #1:
https://example.com/site/index.php
https://example.com/site/
should get converted to short url.
https://example.com/
规则 #2:
https://example.com/
should load full url without expanding.
https://example.com/site/index.php
.
规则 #3:
https://example.com/a/site/index.php
https://example.com/b/site/index.php
https://example.com/a/site/
https://example.com/b/site/
should get converted to short url.
https://example.com/a/
https://example.com/b/
注意: 我想从 URL 中删除 site
文件夹,但我不知道写 .htaccess
,我们将不胜感激。
您可以在站点根目录 .htaccess 中尝试这些规则(假设同级目录中没有其他 .htaccess):
RewriteEngine On
# matches /site/ or /site/index.php or
# /a/site/index.php or /b/site/index.php
# captures a/ or b/ or an empty string in %1
# redirects to /a/ or /b/ or /
RewriteCond %{THE_REQUEST} \s/+([ab]/|)site/(?:index\.php)?[?\s] [NC]
RewriteRule ^ /%1 [L,R=302,NE]
# matches a/ or b/ or empty (landing page)
# rewrites to a/site/index.php or b/site/index.php or site/index.php
RewriteRule ^([ab]/)?$ site/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.+?)/?$ .php [L]