.htaccess重写规则后,PHP文件无法访问$ _GET变量 [英] PHP file not able to access $_GET variable after .htaccess rewrite rule

查看:78
本文介绍了.htaccess重写规则后,PHP文件无法访问$ _GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上的URL中有许多PHP页面带有查询字符串.我正在尝试更改SEO的URL,为此,我使用的是.htaccess文件.

I have a number of PHP pages with query strings in the URLs on my site. I'm trying to change the URLs for SEO, to do this I'm using an .htaccess file.

.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:

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

这使我可以访问category/1的页面category.php?id=1,与我有重写规则的其他页面相同.但是我的PHP脚本无法再从URL访问$ _GET变量,因此它可以正确重写,但现在无法显示任何内容?

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. 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?

我做错了什么,还是有更好的方法来实现这一目标?任何帮助,将不胜感激.

Am I doing something wrong or is there a better way to achieve this. Any help with this would be greatly appreciated.

推荐答案

您将需要关闭MultiViews选项,并对规则进行一些重构.

You will need to turn MultiViews option off and also do little bit refactoring of your rules.

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]

选项MultiViews(请参见 http://httpd.apache.org /docs/2.4/content-negotiation.html )由Apache's content negotiation module使用,该Apache's content negotiation module之前 mod_rewrite运行,并使Apache服务器匹配文件扩展名.因此,如果/file是URL,则Apache将提供/file.html.

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. So if /file is the URL then Apache will serve /file.html.

这篇关于.htaccess重写规则后,PHP文件无法访问$ _GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆