Htaccess rewriterule,在此服务器上找不到文件

时间:2015-04-20 17:30:23

标签: .htaccess mod-rewrite

我是整个htaccess语言的新手,但我觉得我接近我想要的。

事实:

parts is a real folder/directory
anotherpart is a real folder/directory
there are no other files in <root> other then index.php
there are no files in the parts folder/directory

文件夹/目录结构:

<root>/index.php
<root>/parts/anotherpart/this.php

我跑了什么.htacces:

RewriteEngine On
RewriteBase /
RewriteRule ^parts/anotherpart/(.*)$ /$1  [R=301,L]
RewriteCond %{HTTP_HOST} ^www.somewebby.it$ [NC]
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]

结果:

always having https and no www
hiding parts/anotherpart and showing https://somewebby.it/this.php

问题:

The requested URL /this.php was not found on this server.

1 个答案:

答案 0 :(得分:2)

您可以在htaccess(必须位于根文件夹中)中替换当前代码

RewriteEngine On
RewriteBase /

# if "www" or http -> redirect to https://domain.com/xxx
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]

# hide "parts/anotherpart/"
RewriteCond %{THE_REQUEST} \s/parts/anotherpart/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]

# silently rewrite back to "parts/anotherpart/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/parts/anotherpart/$1 -f
RewriteRule ^(.+)$ parts/anotherpart/$1 [L,QSA]