php - Rewrite rule is not working when I'm getting the value of GET in url
问题描述
.htaccess file
RewriteEngine on
#FIRST RULE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#2ND RULE
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?page=$1 [L]
#3RD RULE
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./edit-page?edit=$1
Now let me explain the code above.
FIRST rewrite rule will remove all .php
extension, so if I have http://localhost:8888/index.php
I can access it with http://localhost:8888/index
without .php extension
SECOND rewrite rule will convert this link:
http://localhost:8888/CodeArk/?page=introduction-page
into
http://localhost:8888/CodeArk/introduction-page
The problem is the last rule (3rd RULE).
it suppose to convert this link:
http://localhost:8888/CodeArk/edit-page/?edit=introduction-page
into
http://localhost:8888/CodeArk/edit-page/introduction-page
Now this doesnt works because when I tried to get the value of edit
in edit-page.php file
edit-page.php
echo "YOUR EDIT IS: "$_GET['edit'];
the output is
YOUR EDIT IS: edit-page/introduction-page.php/introduction-page
What happened? the output should be only this
YOUR EDIT IS: introduction-page
I suspect that my rewrite rules is the cause, but I dont know the cause. Please help
解决方案
Finally solved it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?page=$1 [L]