首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重写时$_GET丢失

重写时$_GET丢失
EN

Stack Overflow用户
提问于 2015-08-11 20:01:53
回答 2查看 82关注 0票数 2

在我的htaccess文件中重写时,我在$_GET请求中释放了ID。

我的重写规则

代码语言:javascript
复制
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

当我只想让url看起来像这样时,如何才能传递$_GET ID:

example.com/post/Post-6

完整的htaccess文件:

代码语言:javascript
复制
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]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-11 20:16:10

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

代码语言:javascript
复制
Options -MultiViews

选项MultiViewsApache's content negotiation module使用,该选项在 mod_rewrite之前运行,并使Apache服务器与文件扩展名匹配。因此,/file可以在URL中,但它将服务于/file.php

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

代码语言:javascript
复制
RewriteRule ^post/(\d+)/([^/]+)/?$ post.php?id=$1&name=$2 [NC,QSA,L]
票数 0
EN

Stack Overflow用户

发布于 2015-08-11 20:07:20

可能是

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31950916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档