使用 htaccess 更改 url

使用 htaccess 更改 url

我的网站上的所有网址都有网址:

www.domain.com/name+name+name+name

如何将所有 url 更改为:

www.domain.com/name-name-name-name

我的 htaccess 是这样的:

<FilesMatch "\.(htaccess|tpl)$">
Order Allow,Deny
Deny from all
</FilesMatch>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.* /index.php [L]
Options -Indexes

答案1

如果你希望重定向将所有包含(加号)的旧 URL 替换为包含(连字符)+的新 URL (即替换为),然后您可以在现有的 mod_rewrite 指令之前执行以下操作:-+-.htaccess

# Replace all "+" with "-" with repeating "internal rewrite" and set env var "REPLACED"
RewriteRule (.*)\+(.*) $1-$2 [N,E=REPLACED:1]

# If env var "REPLACED" is set then issue final "external redirect" to the new URL
RewriteCond %{ENV:REPLACED} .
RewriteRule (.+) /$1 [R,L]

澄清一下...严格来说,这并不是“用.htaccess”更改 URL。以上内容假设 URL 已“在您的应用程序中更改”(使用连字符代替加号),并且上述重定向是为了使 SEO 受益,其中旧 URL(包含加号)已被索引、链接和收藏。

相关内容