htaccess重定向问题,在URL后附加变量

时间:2015-07-31 09:23:11

标签: php apache .htaccess mod-rewrite redirect

我已经看过几个主要基于此的线程。 我遇到的问题是我将所有非www URL重定向到www然后将http推送到https,因此我的最终URL以https://www开头。

从那里开始,内部页面上的重定向开始将变量附加到URL的末尾,IE: http://www.domain.co.uk/internal-doors/range - 重定向到https://www.domain.co.uk/internal-doors/range格式为https://www.domain.co.uk/internal-doors/range?id=range时 - 在最后附加我的所有网址变量。

我的完整htaccess如下:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* – [F,L]

RewriteBase /
Options -Indexes
RewriteRule ^(admin|blog|images|imports)($|/) - [L]

ErrorDocument 404 /404.php

RewriteRule ^internal-doors/([^/\.]+)/([^/\.]+)/?$ offering.php?id=$1&o=$2 [NC,QSA]
RewriteRule ^internal-doors/([^/\.]+)/?$ detail.php?id=$1 [NC,QSA]
RewriteRule ^information/([^/\.]+)/?$ content.php?p=$1 [L]
RewriteRule ^external-doors/([^/\.]+)/([^/\.]+)/?$ offering.php?id=$1&o=$2 [NC,QSA]
RewriteRule ^external-doors/([^/\.]+)/?$ detail.php?id=$1 [NC,QSA]
RewriteRule ^accessories/([^/\.]+)/?$ detail.php?id=$1 [NC,QSA]

RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ range.php?s=$1&r=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d
RewriteRule ^([^/\.]+)/?$ category.php?s=$1 [L,QSA]

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 2592000 seconds"
  ExpiresByType application/x-javascript "access plus 2592000 seconds"
  ExpiresByType application/javascript "access plus 2592000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=2592000, private"
</FilesMatch>
<FilesMatch "\\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

希望这是有道理的。关于这一点还有一些其他的线索,但我看不到明确的确定答案,在重定向结束时停止URL的追加。

提前致谢。

1 个答案:

答案 0 :(得分:1)

为了澄清我给出的摘要,规则处理的顺序很重要。首先执行https协议,然后处理处理动态URL重写的任何其他重写规则。

# Enforce https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]

# other rules