带有url变量的mod_rewrite

时间:2015-07-27 00:08:54

标签: php regex apache .htaccess mod-rewrite

我试图转换以下内容:

http://example.com/gallery?page=1http://example.com/gallery/2(不延期)

http://example.com/gallery.php?page=1http://example.com/gallery/2.php分机)

我已经编写了代码以删除网址扩展程序,但我似乎无法解决这个问题。

我尝试过以下方法:

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

2 个答案:

答案 0 :(得分:2)

这应该是你的全部.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. 在htaccess中订购您的规则非常重要
  2. MultiViews需要关闭
  3. index.php规则应该是最后一个规则,因为规则处理所有非文件/非目录。

答案 1 :(得分:0)

尝试:

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]