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

编程入门 行业动态 更新时间:2024-10-15 12:28:50
本文介绍了.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(请参见 httpd.apache /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 httpd.apache/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文件无法访问$

本文发布于:2023-10-11 14:13:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   无法访问   规则   文件   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!