htaccess文件:用hypen替换空格+指向新网址的链接
我想用连字符替换URL中的任何空格
I'd like to replace any space in my URLs with a hypen
这是我完整的htaccess文件:
This is my complete htaccess file:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^archive/([^/]+)$ news?title=$1
问题是我的标题经常包含空格,因此会返回以下网址:
The problem is that my title often contains spaces so it returns this url:
example.com/archive/this%20is%20a%20test
我要创建的网址:example.com/archive/this-is-a-test
Url that I'd like to create: example.com/archive/this-is-a-test
我读到我可以添加 include删除任何空格,但我的尝试没有用...
I read that I could include a "\" to remove any spaces, but my attempts didn't work...
一个相关的问题:如何从另一个网页链接到该新网址?我无法使用以下php原因,该原因只会返回带有%20符号的网址:
A related question: how do I link to this new url from another webpage? I cannot use the following php cause that would just return the url with the "%20" symbols:
<a class="readmore" href="/archive/<?php echo $row['title'];?>
将其放在您的htaccess文件中
place this in your htaccess file
RewriteEngine On
使用+
remove spaces from start or after with +
RewriteRule ^(.*/|)[\s+]+(.+)$ $1$2 [L]
使用+
从末尾删除空格
remove spaces from end or before with +
RewriteRule ^(.+?)[\s+]+(/.*|)$ $1$2 [L]
用-代替空格在+
replace spaces by - in betweenwith +
RewriteRule ^([^\s+]*)(?:\s|+)+(.*)$ $1-$2 [L,R]