当我在浏览器中直接输入网址

时间:2017-07-08 14:16:06

标签: php apache .htaccess redirect mod-rewrite

我有这个目录结构

/gallery
/home
    index.php
    (other files here).php
/products
/(other folders here)
index.php

在根index.php中有一个简单的重定向:

<?php header('location: /home/');

确实,home/index.php是网站(我的意思是:HTML,CSS,JS等)开始的地方

现在,由于它是一个动态网站,我需要一些重写规则。在这里,我们的评论可以帮助您理解:

# Add a trailing slash to folders that don't have one
# Wehen user types 'mysite.dev/folder' it is converted to 'mysite.dev/folder/'
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

# Exclude these folders from rewrite process
# Am I right here? Is this rule right?
RewriteRule ^(admin|ajax|assets|blocks|cache|classes|Connections|cron|editor|inc|intranet|lang|loops|pub)($|/) - [L]

# Remember: the root 'index.php' redirects to '/home' folder: then
# we append the Italian language so that Italian site version is shown as default
RewriteRule ^(/home/)?$ /home/index.php?nLang=it [NC,L]

# Start rewriting rules
RewriteRule ^sfogliabile/([\d]+).htm$                   /flip/browser.php?iCat=$1                              [NC,L]
RewriteRule ^depliant/([\d]+).htm$                      /flip/flyer.php?iSpecial=$1                            [NC,L]
RewriteRule ^([a-z]+)/risultati.htm$                    /home/risultati.php?nLang=$1                           [NC,L,QSA]
RewriteRule ^([a-z]+)/cookie.htm$                       /home/cookie.php?nLang=$1                              [NC,L]
RewriteRule ^([a-z]+)/nojs.htm$                         /home/nojs.php?nLang=$1                                [NC,L]
#RewriteRule ^([a-z]+)/professional/$                   /home/pro.php?nLang=$1                                 [NC,L]
RewriteRule ^([a-z]+)/3/([\w-]+)/$                      /products/index.php?nLang=$1&iModule=3                 [NC,L]       # it/3/prodotti/, en/3/products/
RewriteRule ^([a-z]+)/3/([\w-]+)/([\d]+)/([\w-]+).htm$  /products/details.php?nLang=$1&iData=$3&iModule=3      [NC,L]       # it/3/prodotti/1/prodotto-x.htm, en/3/products/1/product-x.htm
RewriteRule ^([a-z]+)/4/([\w-]+)/$                      /foreground/index.php?nLang=$1&iModule=4               [NC,L]       # it/4/primo-piano/, en/4/foreground/
RewriteRule ^([a-z]+)/4/([\w-]+)/([\d]+)/([\w-]+).htm$  /foreground/details.php?nLang=$1&iData=$3&iModule=4    [NC,L]       # it/4/primo-piano/1/articolo-x.htm, en/4/foreground/1/article-x.htm
RewriteRule ^([a-z]+)/5/([\w-]+)/$                      /specials/index.php?nLang=$1&iModule=5                 [NC,L]       # it/5/speciali/, en/5/specials/
RewriteRule ^([a-z]+)/5/([\w-]+)/([\d]+)/([\w-]+).htm$  /specials/details.php?nLang=$1&iData=$3&iModule=5      [NC,L]       # it/5/speciali/1/articolo-x.htm, en/5/specials/1/article-x.htm
RewriteRule ^([a-z]+)/6/([\w-]+)/$                      /gallery/index.php?nLang=$1&iModule=6                  [NC,L]       # it/6/gallerie/, en/6/galleries/
RewriteRule ^([a-z]+)/6/([\w-]+)/([\d]+)/([\w-]+).htm$  /gallery/details.php?nLang=$1&iData=$3&iModule=6       [NC,L]       # it/6/gallerie/1/galleria-x.htm, en/6/galleries/1/gallery-x.htm
RewriteRule ^([a-z]+)/([\w-]+)/([\d]+)/([\w-]+).htm$    /home/page.php?nLang=$1&iData=$3                       [NC,L,QSA]   # it/azienda/1/pagina-x.htm, en/company/1/page-x.htm

# solution from http://stackoverflow.com/a/39931184/160044
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME}     !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME}     !-f
RewriteRule ^([a-z]+)/$             home/index.php?nLang=$1     [L,QSA,NC]

这很好,似乎一切都按预期工作,但只有一个警告。 当我在浏览器中输入mysite.dev + [Enter]时,我被重定向到mysite.dev/home/index.php?nLang=it,暴露了我不想要的东西并破坏了重写过程:为什么会发生?我哪里错了?

当我删除/home/index.php?nLang=it部分并按[回车]时,一切都很好:mysite.dev部分就是我想要的(或者至少是{{1}也可以。)

想要查看Apache errors.log?这是(不知道为什么它也考虑不应该考虑的文件夹,如mysite.dev/it/assets),只需用{{1}替换pub }:

mysite.dev

请帮忙吗?感谢

编辑:呃 - 哦......我注意到,如果我把所有其他规则放在下面的规则之前(现在不是它之后),一切似乎都有效......这可能是原因吗?如果是的话,我是否被迫将其他所有内容放在其他地方或是否有解决方法?

ljpharma.dev

1 个答案:

答案 0 :(得分:0)

在内部重写之前,您应该始终放置重定向,即使我不清楚为什么您会根据这些规则进行域更改。

重写url时,Apache会反复浏览.htaccess文件,直到url不再更改为止。为什么这很重要?那么,请看以下示例:

#The internal rewrite
RewriteRule ^foo$ /bar [L]

#The external redirect
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

当我们输入网址http://localhost/foo时,Apache将完成规则并匹配第一个规则。现在,它将使用网址http://localhost/bar完成规则再次并匹配第二条规则,然后重定向到https://localhost/bar。遇到R标志后,Apache不会再次评估规则,并且在当前的规则运行停止后,例如,遇到L标志或到达.htaccess文件的末尾。

如果您将重定向放在首位,则会将http://localhost/foo与重定向规则匹配,这将使浏览器请求https://localhost/bar。现在,Apache将在新请求中匹配该URL,该请求将不再匹配任何重定向,但将匹配内部重写,并在内部尝试提供资源bar

旁注:在.htaccess文件中,匹配的资源保证不会以/开头,因为.htaccess文件位于目录中,并且此目录从匹配的URL中被剥离并重新应用重写后。因此,RewriteRule ^(/home/)?$ ...规则有点愚蠢,因为括号内的部分保证永远不会匹配。如果您希望它匹配以RewriteRule ^(home/?)?$ ...开头的路径,请将其更改为home