mod_rewrite with url variables


mod_rewrite with url variables

我正在尝试转换以下内容:

http://example.com/gallery?page=1到http://example.com/gallery/2(不带扩展名)

http://example.com/gallery.php?page=1 to http://example.com/gallery/2(带.php扩展名)

我已经写了我的代码来删除url扩展,但我似乎不能弄清楚。

我试过以下方法:

RewriteRule ^gallery/([^/]+)/?$ gallery.php?page=$1
RewriteRule ^gallery/page/([0-9])[/]?$ gallery.php?page=$1
RewriteRule ^gallery/page/([0-9])[/]?$ gallery?page=$1
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?page=$1 [NC]
RewriteRule ^gallery/([0-9]+)/?$ gallery?page=$1 [NC,L] 

这些都没有成功。

我已经读到,L告诉脚本停止,如果它的工作,我需要删除L从一切除了最后一行在.htaccess文件?

这是我的完整.htaccess到目前为止:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}'s(.*?/+)index'.php(?:/(.*))?['s?] [NC]
RewriteRule ^ %1%2 [L,R]
# internally add index.php to all URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule !^index'.php index'.php%{REQUEST_URI} [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}'s(.*?/+)index(?:/(.*))?['s?] [NC]
RewriteRule ^ %1%2 [L,R]
# internally add index to all URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule !^index index%{REQUEST_URI} [L,NC]
RewriteRule    ^gallery/([0-9]+)/?$    gallery.php?page=$1    [NC]    # Handle gallery requests
RewriteRule    ^gallery/([0-9]+)/?$    gallery?page=$1    [NC,L]    # Handle gallery requests
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php

这应该是你完整的。htaccess:

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} /gallery'.php'?page=([^&'s]+) [NC] 
RewriteRule ^ /gallery/%1? [R=302,L]
RewriteCond %{THE_REQUEST} 's/+(.*?/)?(?:index)?(.*?)'.php['s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# To internally forward /dir/file to /dir/file.php
RewriteRule ^gallery/([^/]+)/?$ gallery.php?page=$1 [QSA,L,NC]
RewriteCond %{DOCUMENT_ROOT}/$1'.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule !^index'.php/ index'.php%{REQUEST_URI} [L,NC]
    在中,规则的排序很重要
  1. MultiViews需要关闭
  2. index.php规则应该是最后一个,因为它处理所有非文件/非目录。

尝试:

RewriteEngine On
RewriteCond %{THE_REQUEST} /gallery'.php'?page=([^'s]+) [NC] 
RewriteRule ^ gallery/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([^/]+)/?$ gallery.php?page=$1 [QSA,L,NC]