htaccess文件:用连字符替换空格+链接到新网址

时间:2014-05-30 14:03:20

标签: php .htaccess rewrite space hyphen

我想用我的网站替换我的网址中的任何空格

这是我完整的htaccess文件:

Options -MultiViews

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^archive/([^/]+)$ news?title=$1

问题是我的标题通常包含空格,因此返回此网址:

example.com/archive/this%20is%20a%20test

我想创建的网址:example.com/archive/this-is-a-test

我读到我可以包含一个“\”来删除任何空格,但我的尝试不起作用......

相关问题:如何从其他网页链接到此新网址?我不能使用以下php原因只返回带有“%20”符号的URL:

<a class="readmore" href="/archive/<?php echo $row['title'];?>

1 个答案:

答案 0 :(得分:0)

将其放在你的htaccess文件中

RewriteEngine On

使用+

从开始或之后删除空格
RewriteRule ^(.*/|)[\s+]+(.+)$ $1$2 [L]

使用+

从末尾或之前删除空格
RewriteRule ^(.+?)[\s+]+(/.*|)$ $1$2 [L]

用〜+ / / h1>替换空格
RewriteRule ^([^\s+]*)(?:\s|+)+(.*)$ $1-$2 [L,R]

相关问题