【问题标题】:htaccess, Regex, Rewrite URLhtaccess,正则表达式,重写 URL
【发布时间】:2014-06-26 23:24:23
【问题描述】:

嗯,我的 .htaccess 有问题,我想重写这个:

http://domain.com/a/thread-103518.html#103518

到这里

http://domain.com/a/103518

换句话说...我想删除“线程-”以及“。”之后的所有内容。 /a/ 必须是可变的...还有其他论坛有 /bla/ 和 /code/

你有什么线索吗?谢谢

我现在的 .htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
重写规则 ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index.php
RewriteRule ^index.php/?(.*) /$1 [L,R=301]

【问题讨论】:

  • 请澄清哪个 URL 是“来自野外”的 URL,哪个是您想要的 .htaccess 输出。我经常看到“将 [动态格式] URI 重写为 [SEO 格式]”的请求,而所需的操作实际上是相反的。如果thread-103518.html是真实页面,103518是SEO格式,你想把后者改写成前者。

标签: regex .htaccess url rewrite


【解决方案1】:

将此添加到DOCUMENT_ROOT 中的.htaccess 文件中

RewriteEngine On
RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L,R]

在 Apache 2.2 和 2.4 中测试:)

这假定 mod_rewrite 安装了并且激活了 htaccess 文件。 如果您不确定,要检查是否安装了 mod_rewrite,请查看phpinfo(); 输出中的已安装模块列表 默认情况下,不为 htaccess 文件启用 mod_rewrite。如果您正在管理自己的服务器,请打开 httpd.conf 并确保 webroot 目录块包含以下行之一:AllowOverride FileInfoAllowOverride All

【讨论】:

  • 感谢您的快速回答...重定向工作,但现在我遇到了 DC 问题。我的 .htaccess RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php RewriteRule ^index \.php/?(.*) /$1 [L,R=301] RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L] domain.com/a/thread-103518.html#103518 AND domain.com/a/103518 现在可以使用,但我只想要第二个 URL
  • 我以为你说http://domain.com/a/thread-103518.html#103518 应该重定向到http://domain.com/a/103518。不明白,这不是问题了吗?
  • 该链接现在可以使用,但您可以使用 domain.com/a/thread-103518.html#103518domain.com/a/103518 进入该站点。单击domain.com/a/thread-103518.html#103518 链接,我想要domain.com/a/103518 上的重定向和URL 重写
  • 我想我明白了。我在标志[DPI,L,R] 中添加了一个R。现在它会重定向,浏览器中的 url 也会改变。让我知道这是否是您正在寻找的。 :)
  • 完美!使用 R 标签,它可以完美运行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多