使用.htaccess [#Q2.]修改URL [英] Modify URL with .htaccess [#Q2.]

查看:106
本文介绍了使用.htaccess [#Q2.]修改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 .htaccess 修改服务器上的URL,但是我遇到了一些问题.

I want to modify the URL on my server using .htaccess but I'm facing some issues.

在我的上一个问题中,我描述了我想要的所有修改.我想在以前的规则旁边添加一些新规则.

In my previous question I described all the modifications I wanted. I want add some new rules along side the previous rules.

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]/)?$ $1site/index.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

新规则1:

https://example.com/site/index.php (已经存在)

https://example.com/site/index (不是)

https://example.com/site/(已经存在)

应该转换为短网址.

https://example.com/

新规则2:

https://example.com/a/site/index.php (已经存在)

https://example.com/a/site/index (不是)

https://example.com/a/site/(已经存在)

应该转换为短网址.

https://example.com/a/

新规则3:

https://example.com/site/any.php (不是)

https://example.com/site/any (不是)

应该转换为短网址.

https://example.com/any/

新规则4:

https://example.com/any/(不是)

应加载完整的网址而不进行扩展.

should load full url without expanding.

https://example.com/site/any.php

请注意:: any.php 是动态的,表示它是([[ww ++).php ",我不这样做.不知道写 .htaccess ,任何帮助将不胜感激.

Note that: The any.php is dynamic, means it's a ([\w]+).php and I don't know to write .htaccess, any help would be much appreciated.

推荐答案

您可以在网站根.htaccess中尝试以下规则:

You may try these rules in your site root .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]

# removes site/ from URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+site/(\S+?)(?:\.php)?/?\s [NC]
RewriteRule ^ /%1/ [L,R=302,NE]

# remove .php from all URLs
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,NE,L]

# add trailing slash to all URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [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]/)?$ $1site/index.php [L,NC]

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

# adds .php internally to /site/any
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/site/$1.php -f
RewriteRule ^(.+?)/?$ site/$1.php [L]

每条规则前都有注释,以阐明其作用.

There are comments before each rule to clarify what it does.

这篇关于使用.htaccess [#Q2.]修改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆