【问题标题】:Removing part of URL but don't redirect with htaccess删除部分 URL 但不使用 htaccess 重定向
【发布时间】:2016-02-24 10:23:34
【问题描述】:

所以我正在研究但还没有解决的问题!

例如网址是http://someurl.com/brands/brand 我想要完成的是 htaccess 让 te 请求转到 url:http://someurl.com/brands/brand 但从 url 中删除“品牌”部分。

另外,如果 url:http://someurl.com/brands 被称为页面“品牌”,则需要显示。所以只有在 /brands/ 后面有品牌时才需要做一个 URL 重写为http://someurl.com/brand

我已经尝试过了,但这篇文章会重定向到不存在的新位置。

RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]

所以我需要上面没有重定向,它只需要重写 URL。 任何帮助将不胜感激,谢谢!

现在这是我完成重写的 htaccess 部分!

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]

# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]

RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting rewrite


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
    RewriteRule ^ /%1 [R=302,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]
    

    【讨论】:

    • 检查完整的 htaccess 文件,我仍然有问题。这是它说“onze-merken”的部分
    • 我收到 404 错误,因为它仍在重定向到 /brand 页面!
    • 否,此规则不会影响http://domain.com/onze-merken URL。在测试此更改之前,您应该完全清除浏览器缓存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2013-03-04
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多