本文介绍了友好的URL空格连字符和放大器;大写改为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换空间在动态URL使用连字符,如果有URL中的任何大写字母那就让他们小写。

空格连字符例如

www.site.com/article/id/ 标题放在这里 - > www.site.com/article/id/title-goes-here

大写字母改为小写例如:

www.site.com/article/id/ 标题-GOES-这里 - > www.site.com/article/id/ 标题去,这里

我目前的.htaccess文件中的规则。

  RewriteEngine叙述上

#主机名前加WWW
的RewriteCond%{HTTP_HOST} ^网站\ .CO $ [NC]
重写规则^ HTTP://www.% {HTTP_HOST}%{REQUEST_URI} [R = 302,L,NE]

#如果在文章页面,获得蛞蝓使之成为友好的URL
?的RewriteCond%{THE_REQUEST} \ S /条\ .PHP \ article_uid =([^&功放;] +)及article_title =([^&放大器; \] +)
重写规则^ /条/ 1%/ 2%/? [L,R = 302,NE]

#如果请求与PHP页面然后删除扩展
的RewriteCond%{THE_REQUEST} \ S / +(。+?)\ PHP [\ S?] [NC]
重写规则^ /%1 / [R = 302,L,NE]

#强制斜线添加
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{THE_REQUEST} \ S / +([^] + [^ /])[\ S?] [NC]
重写规则^ /%1 / [R = 302,L]

#允许页面方向改变蛞蝓到SEO友好的URL
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则?(?:^ | /)条/([^ /] +)/([^ /] +)/ $ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]

#默默地改写到Web根目录
的RewriteCond%{REQUEST_URI}!/ Webroot公司/ [NC]
重写规则^ /根目录%{REQUEST_URI} [L]

#的.php分机隐藏
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME -f .PHP
重写规则^(。+?)/?$ $ 1.PHP [L]
 

解决方案

有关空间连字符转换在 /条/ 的URI,你可以试试这个规则:

 重写规则^(文章)/([^] *)+(。*)$/ $ 1 / $ 2- $ 3 [L,R]
 

插入此规则略高于强制斜杠规则。

PS:对于大小写转换,你将需要访问服务器的配置,因为大小写转换需要在Apache服务器配置一个 RewriteMap指令输入

I'd like to convert spaces in dynamic URLs to use hyphens, and if there are any uppercase letters in the URL then it would make them lowercase.

spaces to hyphens example

www.site.com/article/id/title goes here -> www.site.com/article/id/title-goes-here

uppercase to lowercase example

www.site.com/article/id/Title-Goes-Here -> www.site.com/article/id/title-goes-here

My current .htaccess file rules.

RewriteEngine on

# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]

# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]

# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]

# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
解决方案

For space to hyphen conversion in your /article/ URIs you can try this rule:

RewriteRule "^(article)/([^ ]*) +(.*)$" /$1/$2-$3 [L,R]

Insert this rule just above Force a trailing slash rule.

PS: For case conversion you will need access to server config since case conversion would need a RewriteMap entry in Apache server config.

这篇关于友好的URL空格连字符和放大器;大写改为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:26