本文介绍了仅在URL中用短划线( - )替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将url中的空格更改为破折号( - )而不更改代码?



URL用于标识页面,所以不可能仅仅改变url,因为这个页面将不会被找到,所以 str_replace 在这个解决方案中是不可用的。



使用 str_replace 两次,一次将空间改为破折号并在页面加载器上将其更改回空格也不会起作用,有些页面在页面名称中已经有一个破折号:)

我希望这可以用htaccess来实现,但我不知道如何。
htacces可能在浏览器中显示不同的网址,然后是实际的网址吗?



例如: www.example。 com / pages / hello%20world
应该可视为 www.example.com/pages/hello-world ,但 GET 变量应该仍然是 hello%20world



我当前的.htaccess文件

 < IfModule mod_rewrite.c> 
RewriteEngine On
RewriteCond%{THE_REQUEST} \s / + home / httpd / vhosts / domain.nl / httpdocs / app / View / Products /([^%20] *)%20([ ^ \s] *)[NC]
RewriteRule ^ / home / httpd / vhosts / domain.nl / httpdocs / app / View / Products /%1-%2 [L,NE,R]
RewriteRule^(/ home / httpd / vhosts / domain.nl / httpdocs / app / View / Products /)/([^-]*)-+(.*)$/ $ 1 / $ 2 $ 3[L ,NE,NC]
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*)$ index.php [QSA,L]
< / IfModule>

其中一个产品的示例网址= http://www.example.nl/software/category/details/this%20is%20a%20product-name 您可以在 DOCUMENT_ROOT / .htaccess $ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ /([^%20] *)%20([^ \s] *)[NC]
RewriteRule ^ / path /%1-%2 [L,NE,R]

RewriteRule^(path)/([^ - ] *) - +(。*)$/ $ 1 / $ 2 $ 3[L,NE,NC]


Is it possible to change the space in the url to a dash (-) without changing it in the code?

The URL is used to identify the page, so it is not possible to just change the url because the page will not be found anymore so str_replace is not usable in this solution.

Using str_replace twice, once to change the space to dash and on the page-loader to change it back to spaces isn't going to work either, some pages already have a dash in the pagename :)

I was hoping this could be achived with htaccess but I have no idea how.Is it possible with htacces to show a different url in the browser then the actual url?

For example: www.example.com/pages/hello%20worldshould be visable as www.example.com/pages/hello-world but the GET variable should still be hello%20world.

This is my current .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{THE_REQUEST} \s/+home/httpd/vhosts/domain.nl/httpdocs/app/View/Products/([^%20]*)%20([^\s]*) [NC]
    RewriteRule ^/home/httpd/vhosts/domain.nl/httpdocs/app/View/Products/%1-%2 [L,NE,R]
    RewriteRule "^(/home/httpd/vhosts/domain.nl/httpdocs/app/View/Products/)/([^-]*)-+(.*)$" "/$1/$2 $3" [L,NE,NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Example url to one of the products = http://www.example.nl/software/category/details/this%20is%20a%20product-name

解决方案

You can try something like this in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+path/([^%20]*)%20([^\s]*) [NC]
RewriteRule ^ /path/%1-%2 [L,NE,R]

RewriteRule "^(path)/([^-]*)-+(.*)$" "/$1/$2 $3" [L,NE,NC]

这篇关于仅在URL中用短划线( - )替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:06