在重写时,$ _GET丢失了

时间:2015-08-11 20:01:54

标签: php .htaccess mod-rewrite

在我的htaccess文件中重写我在$ _GET请求中丢失了ID。

我的重写规则

RewriteRule ^post/(.*)$ post.php?id=$1&name=$2&%{QUERY_STRING} [QSA,L]

我的网址是:example.com/post.php?id = 7& name = Post-6

通过我的重写,这个版本有效:example.com/7

但不是example.com/post/Post-6

当我希望网址看起来像这样时,如何让$ _GET ID通过:

example.com/post/Post-6

完整的htaccess文件:

Options -MultiViews

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteBase /

RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [B,L]

RewriteRule ^merchant/$ merchants.php
RewriteRule ^merchant/(.*)/$ search.php?q=merchant:$1:&rewrite=1&%{QUERY_STRING} [B,L]
RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=merchant:$1:&page=$2&rewrite=1&%{QUERY_STRING} [B,L]

RewriteRule ^category/$ categories.php [L]
RewriteRule ^category/(.*)/$ categories.php?path=$1  [L,B]
RewriteRule ^category/(.*)/(.*).html$ search.php?q=category:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L,B]

RewriteRule ^brand/$ brands.php
RewriteRule ^brand/(.*)/$ search.php?q=brand:$1:&rewrite=1&%{QUERY_STRING} [B,L]
RewriteRule ^brand/(.*)/(.*).html$ search.php?q=brand:$1:&page=$2&rewrite=1&%{QUERY_STRING} [B,L]

RewriteRule ^blog\/ blog.php
RewriteRule ^blog\/?p=1 blog\/ [L]
RewriteRule ^post/(.*)$ post.php?id=$1&name=$2&%{QUERY_STRING} [QSA,L]

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# special MIME type for icons - see http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon
AddType image/vnd.microsoft.icon .ico
# now we have icon MIME type, we can use it
# my favicon doesn't change much
ExpiresByType image/vnd.microsoft.icon "access plus 3 months"
# CSS
ExpiresByType text/css "access plus 1 week"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
    <FilesMatch ".(js|xml|css|gz|html)$">
        Header append Vary: Accept-Encoding
    </FilesMatch>
</IfModule>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

2 个答案:

答案 0 :(得分:0)

可能是

  

RewriteRule ^ post /(.*)/(.*)$ post.php?id = $ 1&amp; name = $ 2&amp;%{QUERY_STRING} [QSA,L]

答案 1 :(得分:0)

将此规则放在.htaccess之上以关闭MultiViews

Options -MultiViews

MultiViews使用选项Apache's content negotiation modulemod_rewrite之前运行,并使Apache服务器匹配文件扩展名。因此/file可以在网址中,但它会投放/file.php

这是因为您有这样的规则:post在源和目标URI中是相同的单词:

RewriteRule ^post/(\d+)/([^/]+)/?$ post.php?id=$1&name=$2 [NC,QSA,L]