htaccess文件:用hypen替换空格+指向新网址的链接 [英] htaccess file: replace spaces with hypen + link to new url

查看:128
本文介绍了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]

这篇关于htaccess文件:用hypen替换空格+指向新网址的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆