如何用.htaccess删除部分URL?

wqnecbli  于 2022-11-16  发布在  其他
关注(0)|答案(5)|浏览(167)

目前,我有20+ URL在我的网站在这种格式
http://www.example.net/content/index/mission
我想从所有URL中删除/content/index,因此它们应该如下所示
http://www.example.net/mission
换句话说,我总是从URL中删除/content/index。我相信这真的很简单,但我对Apache不是很有经验。

wko9yo5t

wko9yo5t1#

您需要Apache的重写模块:mod_rewrite .
然后执行以下操作:

RewriteEngine on 
RewriteRule ^content/index/(.*)$ $1

下面是mod_rewrite的官方文档:click

z18hc3ub

z18hc3ub2#

我猜您已经为http://www.example.net/content/index/mission等URL制定了一些重写规则
您需要找到这些规则并添加一个新的规则,该规则使用类似的结构,但对内容/索引部分进行“硬编码”,例如,假设现有的规则是

RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa]

你可能会想制定一个新的规则来拾取/使命,并以类似的方式重写它,但要在现有规则生效之前,例如。

RewriteRule ^mission$ /content.php?param1=index&param2=mission [L,qsa]
RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa]

这些只是示例-这将真正取决于您现有的规则是什么。

dojqjjoe

dojqjjoe3#

# Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]

或者只添加index.html,这取决于你想删除的扩展名,在Apache上需要MOD_REWRITE才能正常工作。谢谢。

gstyhher

gstyhher4#

我还写了一个脚本,你可以把它放在你的服务器上,然后通过互联网浏览器浏览到它,它可以确认你的服务器上是否有mod_rewrite。

qncylg1j

qncylg1j5#

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/removedstring/
RewriteRule ^removedstring/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]

相关问题