【问题标题】:Remove some parts of url by htaccess in apache2 server通过 apache2 服务器中的 htaccess 删除部分 url
【发布时间】:2015-10-21 14:28:45
【问题描述】:

我正在尝试通过 .htaccess 在 ubuntu 14.04 的 apache2 服务器上重写 url。我有以下网址:

localhost/compare/compares/single/16/postoneVSposttwo

但我想要这样:

 localhost/compare/compares/postoneVSposttwo

我已尝试遵循 .htaccess 规则:

RewriteEngine on
RewriteRule ^(.+)/single/.+/(.+)$ $1/$2 [L,NC,R]

但它的网址如下:

localhost/var/www/html/compare/webroot/compares/postoneVSposttwo

我该如何解决这个问题?

【问题讨论】:

    标签: regex apache .htaccess url model-view-controller


    【解决方案1】:

    你可以使用这条规则:

    RewriteEngine on
    
    RewriteRule ^(.+?)/single/[^/]+/(.+)$ /compare/$1/$2 [L,NC,R=302]
    

    【讨论】:

    • 嗨,它可能会起作用。但还是小问题。它会创建一个像这样的网址:localhost/compares/postoneVSposttwo。但我需要像:localhost/compare/compares/postoneVSposttw
    • 嗨,现在还有一个问题。它包括一个额外的“webroot”,如下所示:localhost/compare/webroot/compares/postoneVSposttow
    • 嗨,我在这个页面上的完整 .htaccess 规则如下: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI } ^/(.+?)/single/[^/]+/(.+)$ [NC] RewriteRule 。 /%1/%2 [L,R=302] RewriteRule ^(.*)$ index.php [PT,L]
    • 检查已编辑的答案。确保它位于RewriteEngine on 行的下方,并在清除浏览器缓存后进行测试。
    • 我很少使用 Skype,但您可以在 Twitter/LinkedIn 上与我联系(在我的个人资料中提供了这些链接)
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 2017-07-04
    • 1970-01-01
    • 2013-07-02
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多