htaccess文件:用hypen替换空格+指向新网址的链接

编程入门 行业动态 更新时间:2024-10-24 06:33:22
本文介绍了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/archive/this%20is%20a%20test

我要创建的网址:example/archive/this-is-a-test

Url that I'd like to create: example/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替换空格+指向新网址的链接

本文发布于:2023-11-01 20:27:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   链接   文件   新网址   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!