【问题标题】:.htaccess: rewrite to new URL structure.htaccess:重写为新的 URL 结构
【发布时间】:2016-09-12 21:51:42
【问题描述】:

我正在尝试将以下 URL 重定向到新结构:

  • 之前http://example.com/profile/?name=some-one
  • 之后http://example.com/profile/some-one/

some-name 将根据用户的帐户而变化,因此我希望该部分是动态的。这就是我的.htaccess

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{QUERY_STRING} (^|&)name=(.*)
RewriteRule ^profile/$ /profile/$1/? [L,R=301]

根据online htaccess tester,结果如下:

http://example.com/profile//

任何帮助澄清为什么它没有继承动态some-name?如果重要的话,这适用于已经具有以下内容的 WordPress 网站:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    试试

    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteCond %{QUERY_STRING} (^|&)name=(.*)
    RewriteRule ^(.*)$ /profile/%2/? [R=301,L]
    

    RewriteCond's 中捕获的变量使用 %

    【讨论】:

    • 感谢您指出这一点。第一个RewriteCond 有必要吗?我在.htaccess tester 上对其进行了测试,我仍然得到了预期的结果。我需要第一行吗?
    • @EthanJinksO'Sullivan 仅当您有多个需要不同规则的主机时,否则不需要。只是为了完整性而添加。
    • 感谢澄清,不胜感激
    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2012-03-23
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多