子域

时间:2015-10-20 17:59:54

标签: php apache .htaccess mod-rewrite

我创建了一个子域,指向我的根目录下的文件夹,如下所示;

RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

我现在可以像/blog

那样访问http://blog.example.co.uk/index.php文件夹的文件了

/blog文件夹包含一个包含脚本的/php文件夹。 我无法像/php

那样访问http://blog.example.co.uk/php/test.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。漂亮的网址规则不起作用。

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

1 个答案:

答案 0 :(得分:0)

请尝试以下规则:

### Error 404 redirect
ErrorDocument 404 /404page.php

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# 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]

# 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 externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=302,L]

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# 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]

### 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]    

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]