简体   繁体   English

.htaccess 重写规则后,PHP 文件无法访问 $_GET 变量

[英]PHP file not able to access $_GET variable after .htaccess rewrite rule

I have a number of PHP pages with query strings in the URLs on my site.我的站点上的 URL 中有许多带有查询字符串的 PHP 页面。 I'm trying to change the URLs for SEO, to do this I'm using an .htaccess file.我正在尝试更改 SEO 的 URL,为此我使用了 .htaccess 文件。

I'm new to .htaccess so this is fairly basic and I'm not sure if I'm doing this right or if there is a better way to do it.我是 .htaccess 的新手,所以这是相当基本的,我不确定我是否做得对,或者是否有更好的方法来做到这一点。

My .htaccess file:我的 .htaccess 文件:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteCond %{REQUEST_URI} !/.css$
RewriteCond %{REQUEST_URI} !/.js$
RewriteCond %{REQUEST_URI} !/.png$
RewriteCond %{REQUEST_URI} !/.jpg$

RewriteRule ^category/([0-9a-zA-Z]+) category.php?id=$1
RewriteRule ^product/([0-9a-zA-Z]+) product.php?id=$1
RewriteRule ^page/([0-9a-zA-Z]+) page.php?page_id=$1

This allows me to access the page category.php?id=1 at category/1 the same for the other pages I have rewrite rules for.这允许我访问category/1的页面category.php?id=1与我重写规则的其他页面相同。 However my php script can no longer access the $_GET variable from the URL so it is rewriting correctly but now unable to show any of the content?但是,我的 php 脚本无法再从 URL 访问 $_GET 变量,因此它正在正确重写但现在无法显示任何内容?

You will need to turn MultiViews option off and also do little bit refactoring of your rules.您需要关闭MultiViews选项并对规则进行一些重构。

Options -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^category/([0-9a-zA-Z]+)/?$ category.php?id=$1 [L,NC,QSA]
RewriteRule ^product/([0-9a-zA-Z]+)/?$ product.php?id=$1 [L,NC,QSA]
RewriteRule ^page/([0-9a-zA-Z]+)/?$ page.php?page_id=$1 [L,NC,QSA]

Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html ) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files.选项MultiViews (参见http://httpd.apache.org/docs/2.4/content-negotiation.html )由Apache's content negotiation module ,该Apache's content negotiation modulemod_rewrite之前运行并使 Apache 服务器匹配文件的扩展名。 So if /file is the URL then Apache will serve /file.html .因此,如果/file是 URL,则 Apache 将提供/file.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM