簡體   English   中英

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

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

當我使用這個包含 %20 的 URL 時,我沒有得到預期的響應。

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

這是 .htaccess

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

這是我的 PHP 代碼,當我使用 .htaccess 時,我得到了錯誤的響應,但是當我使用普通 URL 時,我得到了預期的響應。

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

}
echo $ss;

這是我的 .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]

輸出:-

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

RegEx 是幫助 .htaccess 讀取您的 URL 並實現您想要的表達式的表達式。

看看你的 Regex Last line in .htaccess : ([A-Za-z0-9-]+)這意味着只接受字母和數字。 非空格字符

為什么它會導致 %20 出現問題? 因為 %20 表示 URL 中的空格,所以您正在搜索“Orissa Electrical Industries”

所以你也應該接受空格字符,如何

使用\\s ,它表示RegEx 中的空格字符。

它會是這樣的:

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

PS:您可以在此 RegExr 中檢查和使用 Regex

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM