【问题标题】:htaccess cannot load correct contenthtaccess 无法加载正确的内容
【发布时间】:2014-02-01 09:20:46
【问题描述】:

我有 2 个网站:一个站点位于公共 html 文件夹的根目录中,另一个站点位于 WFM 子文件夹中,WFM 子文件夹面临一些问题,即无法重定向到正确的页面而不是显示内容在 domain.com.sg/careers 中,它正在重新加载 主页

奇怪的是图像,JS 和 CSS 返回 200 OK,尽管文件包含 index.php 的内容。通过 Google Chrome 网络开发者工具测试。

domain.com.sg 只是一个虚拟网址。

/home/webfire/public_html/.htaccess 上的代码

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
IndexIgnore *
Options All -Indexes
IndexOptions +SuppressHTMLPreamble

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com.sg  #Not working correctly
RewriteCond %{REQUEST_URI} !^/wfm   #Not working correctly
RewriteRule ^(.*)$ /wfm/$1 [L]      #Not working correctly

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f  #For website located in the public_html folder
RewriteCond %{REQUEST_FILENAME} !-d  #For website located in the public_html folder
RewriteRule ^(.*)$ index.php/$1/ [L] #For website located in the public_html folder

RewriteCond %{REQUEST_METHOD} ^PUT$ [OR]
RewriteCond %{REQUEST_METHOD} ^MOVE$
RewriteRule ^/files/(.*)\.php /files/$1.nophp



RewriteCond %{HTTP_REFERER} !^http://domain.com.sg/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.sg$ [NC]

RewriteCond %{HTTP_REFERER} !^http://www.domain.com.sg/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.sg$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    试试这个代码:

    IndexIgnore *
    Options All -Indexes
    IndexOptions +SuppressHTMLPreamble
    
    RewriteEngine On
    RewriteBase /
    
    # existing rule to block images hot linking 
    RewriteCond %{HTTP_REFERER} !^http://domain.com.sg/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://domain.com.sg$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.domain.com.sg/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.domain.com.sg$ [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]
    
    # for subdomain forward to /wfm/
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.sg$
    RewriteRule ^((?!wfm/).*)$ /wfm/$1 [L,NC]
    
    # existing rule to handle PUT/MOVE reqquest
    RewriteCond %{REQUEST_METHOD} ^PUT$ [OR]
    RewriteCond %{REQUEST_METHOD} ^MOVE$
    RewriteRule ^files/(.*)\.php /files/$1.nophp [L]
    
    # is PHP file exists for given URI then use it
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^.]+)/?$ $1.php [L]
    
    # forward rest to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1/ [L]
    

    【讨论】:

    • 太好了!您是否介意为每一行添加注释,因为我看到了对我来说是新的新语法。
    • 现在在每条规则之前添加了 cmets。
    猜你喜欢
    • 2011-11-15
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多