【问题标题】:mod_rewrite with url variables带有 url 变量的 mod_rewrite
【发布时间】:2015-07-27 00:08:54
【问题描述】:

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

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

http://example.com/gallery.php?page=1http://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


【解决方案1】:

这应该是您的完整 .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 规则应该是最后一条,因为该规则正在处理所有非文件/非目录。

【讨论】:

    【解决方案2】:

    试试:

    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]
    

    【讨论】:

    • 500 内部服务器错误,我找不到错误日志。我正在使用 Godaddy 作为主机。
    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多