![](/img/trans.png)
[英]Apache2 - Directory URL without trailing slash redirecting to different URL
[英]PHP Redirecting to Custom URL if trailing slash is absent or with parameters
如果訪問者使用任何URL(例如index.php?permalink=query
或REQUEST_URI
缺少斜杠,我想重定向到自定義URL
我不明白為什么這對我不起作用。 對於url參數,其工作正常。
以下代碼在做什么?
$requesturl = $_SERVER['REQUEST_URI'];
if($requesturl == "/index.php?permalink=".$query || substr($requesturl, -1) != '/') {
$redirect_requesturl = $baseurl."/".$query."/";
Header( 'HTTP/1.1 301 Moved Permanently' );
Header( 'Location: ' . $redirect_requesturl );exit;
}
當查詢以index.php?permalink=$query
query出現時,它正在重定向,就像URL不包含尾部斜杠表示不重定向一樣。
我的.htaccess如下:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]+)?/$ index.php?permalink=$1&%{QUERY_STRING}
我不能使用.htaccess強制所有URL都帶有斜杠,因為某些重要的URL不包含斜杠。
雖然現在可以在.htaccess中使用以下代碼,但是它會在css或js或otf / ttf / woff等字體文件中添加斜杠,
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|png|jpeg|pdf|doc|docx|css|js|xml|xls|xsl|txt|ico|eot|svg|ttf|woff|woff2|otf|flv|swf)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
如果訪問者使用任何URL(例如index.php?permalink=query
或REQUEST_URI
缺少斜杠,我想重定向到自定義URL
您可以使用以下規則來完成此操作:
RewriteEngine on
# if the requested uri has querystring
RewriteCond %{QUERY_STRING} ^permalink=.+$ [OR]
# or the trailing slash is missing
RewriteCond %{REQUEST_URI} !/$
# redirect the request to /custom_url
RewriteRule ^(.+)$ /custom_url? [L,R]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.