【问题标题】:htaccess removing directory from url makes page not openhtaccess 从 url 中删除目录使页面无法打开
【发布时间】:2014-05-19 01:33:32
【问题描述】:

我在名为“pages”的文件夹中有一个名为 about.php 的页面。

我已成功从 url 中删除了 .php 和目录,但现在页面打不开。

我所有的文件都在 localhost/test/ 文件夹中。 所以过去一切都以 localhost/test/pages/about.php 的形式正常打开 现在它显示 localhost/test/about 并且没有打开。

RewriteBase /test/    
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteRule ^pages/(.*)$ $1 [L,NC,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]

【问题讨论】:

  • 你能澄清一下你真正想要达到的目标吗?您正在尝试将请求重定向到 mysite.com/about 到内部文件 /test/about.php
  • 是的。 localhost/test/about 内部重定向到 localhost/test/pages/about.php。我似乎在 url 中工作,但页面没有加载

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

你可以使用:

RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{THE_REQUEST} /pages/ [NC]
RewriteRule ^pages/(.+)$ $1 [L,NC,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}\.php -f [NC]
RewriteRule ^([^/]+)/?$ pages/$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 2020-05-11
    • 2011-09-24
    • 2012-03-19
    • 2012-06-25
    • 2019-06-19
    • 1970-01-01
    相关资源
    最近更新 更多