【发布时间】:2015-07-27 00:08:54
【问题描述】:
我正在尝试转换以下内容:
http://example.com/gallery?page=1 到 http://example.com/gallery/2(无扩展名)
或
http://example.com/gallery.php?page=1 到 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 告诉脚本如果它工作就停止,我是否需要从除.htaccess 文件中的最后一行之外的所有内容中删除L?
这是我现在的完整.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
【问题讨论】:
标签: php regex apache .htaccess mod-rewrite