.htaccess htaccess只能更改URL地址

.htaccess htaccess只能更改URL地址,.htaccess,.htaccess,我有一个网站,需要htaccess代码在地址栏中进行此更改,而无需在服务器文件夹中进行任何物理更改 example.com/data/65767.html 成为 example.com/65767 example.com/data/56-45.html 成为 example.com/56-45 鉴于效率极低,您需要确保您的链接被更改为指向没有.html的链接,但是 RewriteEngine On # externally redirect so that the browser shows

我有一个网站,需要htaccess代码在地址栏中进行此更改,而无需在服务器文件夹中进行任何物理更改

example.com/data/65767.html 成为 example.com/65767

example.com/data/56-45.html 成为 example.com/56-45


鉴于

效率极低,您需要确保您的链接被更改为指向没有
.html
的链接,但是

RewriteEngine On

# externally redirect so that the browser shows the non-.html URL
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /data/([0-9-]+)\.html
RewriteRule ^ /%2 [L,R=301]

# internally rewrite the non-.html URI back to the .html URI
RewriteCond %{DOCUMENT_ROOT}/data%{REQUEST_URI}.html -f
RewriteRule ^/?([0-9-]+)$ /data/$1.html [L]
这适用于
/data/
URI。如果您想让它与任何东西一起工作,请将
[0-9-]
替换为


编辑:

我们是否可以将
example.com/65767.html
重定向为
example.com/65767
以及
example.com/data/65767.html

是的,只需将第一条规则更改为:

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /(data/)?([0-9-]+)\.html
RewriteRule ^ /%3 [L,R=301]

效率极低,您需要确保您的链接被更改为指向没有
.html
的链接,但是

RewriteEngine On

# externally redirect so that the browser shows the non-.html URL
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /data/([0-9-]+)\.html
RewriteRule ^ /%2 [L,R=301]

# internally rewrite the non-.html URI back to the .html URI
RewriteCond %{DOCUMENT_ROOT}/data%{REQUEST_URI}.html -f
RewriteRule ^/?([0-9-]+)$ /data/$1.html [L]
这适用于
/data/
URI。如果您想让它与任何东西一起工作,请将
[0-9-]
替换为


编辑:

我们是否可以将
example.com/65767.html
重定向为
example.com/65767
以及
example.com/data/65767.html

是的,只需将第一条规则更改为:

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /(data/)?([0-9-]+)\.html
RewriteRule ^ /%3 [L,R=301]

谢谢,它工作正常,但我需要添加一些其他内容,如果可能的话,我们也可以将example.com/65767.html重定向为example.com/65767,example.com/data/65767.htmlhanks的内容工作正常,但是如果可能的话,我需要添加一些其他内容。我们是否可以将example.com/65767.html重定向为example.com/65767,并将example.com/data/65767.html中的内容重定向到example.com/65767.html