htaccess只更改URL地址

时间:2012-10-17 01:06:59

标签: .htaccess

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

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

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

此致

1 个答案:

答案 0 :(得分:1)

效率极低,您需要确保您的链接更改为指向没有.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/<any number of digits or "-">个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]