【问题标题】:htaccess rule for subfolders in subdomain子域中子文件夹的 htaccess 规则
【发布时间】: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


    【解决方案1】:

    试试这些规则:

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

    【讨论】:

    • 感谢大师。您的代码有效,但它破坏了您昨天帮助我的漂亮 url 代码。请查看我的编辑并提供帮助。谢谢你:)
    • 仍然没有喜悦http://blog.example.co.uk/post/153/title-of-post 转到Error 500 :(
    • 每次更改都会带来新的 URL。 http://blog.example.co.uk/php/test.php URL 发生了什么?如果得到 500 则检查 Apache error.log 并在此处报告错误文本
    • 该网址给出了错误 500,而您的初始答案已解决该问题。但后来它破坏了我友好的网址,现在他们给出了错误 500。
    • 设法让这个工作。所有的规则都是正确的。只好重新排列它们。再次感谢。标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2018-10-19
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多