简体   繁体   English

HTACCESS mod_rewrite查询字符串

[英]HTACCESS mod_rewrite for query string

I am just trying to get mod_rewrite working and I am having a massive mental block where nothing seems to be working. 我只是想使mod_rewrite工作,并且我有一个巨大的思维障碍,似乎没有任何效果。

My .htaccess file hides the file extension of my files and I'm not sure if this is why my rewrite isn't working. 我的.htaccess文件隐藏了文件的文件扩展名,我不确定这是为什么我的重写无法正常工作的原因。

Here is my .htaccess code: 这是我的.htaccess代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?project=$2
Options +FollowSymLinks

With the rewrite rule I am trying to get projects.php?project=1 to display as projects/project/1 使用重写规则,我试图获取projects.php?project = 1以显示为projects / project / 1

Would really appreciate it if someone could enlighten me as to what I am doing wrong. 如果有人能启发我有关我做错的事情,我将不胜感激。

Problem is not ending the rules with L (Last) flag and ordering of your rules. 问题不是以L (最后一个)标志和规则的顺序结束规则。 Replace your code with: 将代码替换为:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?project=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

The RewriteCond s are applied to the first rule only. RewriteCond仅应用于第一条规则。 If you want the same conditions for the second rule you must duplicate them. 如果您希望第二条规则具有相同的条件,则必须复制它们。

Order is important. 顺序很重要。 The first rule is applied first and rewrites /projects/project/1 to /projects/project/1.php and then again to /projects/project/1.php.php and /projects/project/1.php.php.php , ..., because the condition /path/to/projects.php -f is always true. 首先应用第一个规则,然后将/projects/project/1重写为/projects/project/1.php ,然后再次重写为/projects/project/1.php.php/projects/project/1.php.php.php ,...,因为条件/path/to/projects.php -f始终为true。 You must add an additional condition to prevent this endless loop. 您必须添加一个附加条件来防止此无限循环。

The second rule is never tried because of this. 因此,永远不要尝试第二条规则。

Finally, the regular expression ^([^/]+)/([^/]+)/$ doesn't match projects/project/1 . 最后, 正则表达式 ^([^/]+)/([^/]+)/$projects/project/1不匹配。 You must add a trailing (\\d+) to capture the project number as $3 您必须添加尾随(\\d+)才能将项目编号捕获为$3

RewriteRule ^([^/]+)/([^/]+)/(\d+)$ /$1.php?project=$3

or 要么

RewriteRule ^([^/]+)/([^/]+)/(\d+)$ /$1.php?$2=$3

if the rule should be more general. 如果规则应该更一般。

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

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