【发布时间】:2016-01-19 12:34:25
【问题描述】:
我已经创建了一个子域来指向我的根目录上的一个文件夹,如下所示;
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
我现在可以像http://blog.example.co.uk/index.php 一样访问/blog 文件夹中的文件
/blog 文件夹包含一个包含脚本的 /php 文件夹。
我无法像http://blog.example.co.uk/php/test.php 那样访问/php 文件夹
这不起作用。为了让我访问它们,我必须这样做http://www.example.co.uk/blog/php/test.php
以下是我目前的.htaccess
Options +FollowSymLinks -MultiViews
### Turn mod_rewrite on
RewriteEngine On
RewriteBase /
### Prevent rules on script folder in root /php & /blog/php because I remove .php extension from all files
RewriteRule ^(php|blog/php)($|/) - [L]
### To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
### To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
### My subdomain
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
现在我需要创建一个规则,将http://blog.example.co.uk/php 指向/blog/php
当我现在尝试访问 /php 中的文件时,我得到一个 File Not Found 错误
谢谢
这是我完整的 htaccess。漂亮的 url 规则不起作用。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
### Blog subdomain
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
### Careers subdomain
RewriteCond %{HTTP_HOST} ^careers\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/careers/ [NC]
RewriteRule ^(.*)$ /careers/$1 [L]
RewriteRule ^(php|blog/php|careers/php|vendors|blog/vendors)($|/) - [L]
# To remove index.php or index.html from URL
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
RewriteCond %{REQUEST_URI} /index\.php?$ [NC]
RewriteRule ^(.*)index\.php?$ "/$1" [NC,R=301,NE,L]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
# external redirect from actual URL to pretty one (BLOG)
RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /post/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+category(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /category/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?author=([^\s&]+) [NC]
RewriteRule ^ /author/%1? [R=302,L,NE]
# external redirect from actual URL to pretty one (CAREERS)
RewriteCond %{THE_REQUEST} \s/+role(?:\.php)?\?id=([^\s&]+)&role=([^\s&]+) [NC]
RewriteRule ^ /role/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (BLOG)
RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=%2 [L,QSA,NC]
RewriteRule ^category/([\w-]+)/([\w-]+)/?$ category.php?id=$1&title=%2 [L,QSA,NC]
RewriteRule ^author/([\w-]+)?$ author.php?author=$1 [L,QSA,NC]
# internal forward from pretty URL to actual one (CAREERS)
RewriteRule ^role/([\w-]+)/([\w-]+)/?$ role.php?id=$1&role=%2 [L,QSA,NC]
### Error 404 redirect
ErrorDocument 404 /404page.php
【问题讨论】:
标签: php apache .htaccess mod-rewrite