且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用htaccess的删除空格或20%的网址

更新时间:2023-02-23 10:14:20

试试这个:

Options +FollowSymLinks -MultiViews -Indexes

RewriteEngine On
RewriteBase /gadgets/

RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]

RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]

RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]

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

第一条规则将删除所有的空格在一个循环中(使用 [N] ),所以我们不必指定多个的规则集做一个接一个吧。

The first rule removes all the whitespaces in a loop (using [N]) so we don't have to specify multiple RewriteRules doing it one by one now.

下一条规则(与 {ENV:重定向} 状态)是可选的,用来反映在客户端的浏览器使用连字符,以及使所有书签创建链接到网址的正确非whitespaced版本。

The next rule (with {ENV:Redirect} condition) is optional and is used to reflect the use of hyphens on the client's browser as well so that any bookmarks created link to the correct non-whitespaced version of the URL.