[英]mod_rewrite with url variables
I'm trying to convert the following: 我正在尝试转换以下内容:
http://example.com/gallery?page=1 to http://example.com/gallery/2 (without extension) http://example.com/gallery?page=1到http://example.com/gallery/2 (无扩展名)
or 要么
http://example.com/gallery.php?page=1 to http://example.com/gallery/2 (with .php
extension) http://example.com/gallery.php?page=1到http://example.com/gallery/2 (带有
.php
扩展名)
I already have my code written to remove url extensions, but I can't seem to figure this out. 我已经编写了删除url扩展名的代码,但似乎无法弄清楚。
I've tried the following: 我尝试了以下方法:
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]
None of these have worked. 这些都不起作用。
I've read that the L
tells the script to stop if it works, do I need to remove the L
from everything except for the last line in a .htaccess
file? 我已经读到
L
告诉脚本如果它停止运行,我是否需要从.htaccess
文件中最后一行以外的所有内容中删除L
?
Here is my full .htaccess
as of now: 这是我到目前为止的完整
.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
This should be your full .htaccess: 这应该是您的完整.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]
MultiViews
need to be turned off MultiViews
需要关闭 index.php
rules should be last one as that one is handling all non-files/non-directories. index.php
规则应该是最后一个规则,因为它正在处理所有非文件/非目录。 Try : 尝试:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.