【问题标题】:Htaccess - replace .html with a forward slashHtaccess - 用正斜杠替换 .html
【发布时间】:2013-03-29 15:12:21
【问题描述】:

我想重定向这种形式的 URL: /page.html?variable=value&othervar=true&thirdvar=100

对此: /page/?variable=value&othervar=true&thirdvar=100

所以基本上我只想用forward slash替换URL中间的.html,但我需要保留它附带的get字符串。这是我尝试过的:

重写规则 ^page.html(.+)$ /page/$1 [L,R=301]

但这似乎对我不起作用。我最近做了类似的事情,但我不知道我在这里缺少什么。感谢您的任何意见。

【问题讨论】:

  • 哎呀,我应该使用降价系统。我还是新手。谢谢你的帮助,维克多。

标签: regex apache .htaccess redirect mod-rewrite


【解决方案1】:

通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]

# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

【讨论】:

  • 对,伙计!这对我来说非常有效。非常感谢您的快速响应!
  • 我会赞成你的回答,但我没有足够的声誉,哈哈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多