简体   繁体   English

如何在 .htaccess 中重定向带有空格的 URL?

[英]How to redirect URLs with blank space in .htaccess?

When I use this URL that contains %20 I don't get expected response.当我使用这个包含 %20 的 URL 时,我没有得到预期的响应。

localhost/worldsindia/company-search.php?company=Orissa%20Electrical%20Industries

This is with .htaccess这是 .htaccess

localhost/worldsindia/search/Orissa%20Electrical%20Industries

It is my PHP code, When I use .htaccess I get wrong response but when I use normal URL I get the expected response.这是我的 PHP 代码,当我使用 .htaccess 时,我得到了错误的响应,但是当我使用普通 URL 时,我得到了预期的响应。

if(isset($_GET['company'])){
    $ss = $_GET['company'];    

}
echo $ss;

This is my .htaccess file这是我的 .htaccess 文件

RewriteEngine On

<ifModule mod_headers.c>
Header set Connection keep-alive 
</ifModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
#AddOutputFilterByType DEFLATE text/plain
#AddOutputFilterByType DEFLATE text/html
#AddOutputFilterByType DEFLATE text/xml
#AddOutputFilterByType DEFLATE text/css
#AddOutputFilterByType DEFLATE application/xml
#AddOutputFilterByType DEFLATE application/xhtml+xml
#AddOutputFilterByType DEFLATE application/rss+xml
#AddOutputFilterByType DEFLATE application/javascript
#AddOutputFilterByType DEFLATE application/x-javascript

RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^([^\.]+)/?$ $0.php [NC,L]

RewriteRule ^search/([A-Za-z0-9-]+)  company-search.php?company=$1 [NC,L]

Output:-输出:-

  1. With .htaccess is Orissa. .htaccess 是奥里萨邦。
  2. With normal URL(localhost/worldsindia/company-search.php?company=Orissa%20Electrical%20Industries) Orissa Electrical Industries使用普通 URL(localhost/worldsindia/company-search.php?company=Orissa%20Electrical%20Industries) Orissa Electrical Industries

RegEx is the expression that helps .htaccess to read your URL and achieve what you want. RegEx 是帮助 .htaccess 读取您的 URL 并实现您想要的表达式的表达式。

Look at your Regex Last line in .htaccess : ([A-Za-z0-9-]+) It means accepting ONLY alphabet And digits.看看你的 Regex Last line in .htaccess : ([A-Za-z0-9-]+)这意味着只接受字母和数字。 NOT SPACE CHARACTER非空格字符

Why it cause problem when it comes to %20 ?为什么它会导致 %20 出现问题? Because %20 means space in URL , You are searching "Orissa Electrical Industries" .因为 %20 表示 URL 中的空格,所以您正在搜索“Orissa Electrical Industries”

So you should accept space character too, How ?所以你也应该接受空格字符,如何

With \\s , it means Space character in RegEx .使用\\s ,它表示RegEx 中的空格字符。

It will be like this :它会是这样的:

RewriteRule ^search/([A-Za-z0-9-\s]+)  company-search.php?company=$1 [NC,L]

PS: You can check and play with Regex in this RegExr PS:您可以在此 RegExr 中检查和使用 Regex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM