【问题标题】:PHP - .htaccess Clean URLs not functioningPHP - .htaccess 清理 URL 不起作用
【发布时间】:2015-10-08 15:35:49
【问题描述】:

出于测试目的,我设置了 index.php,其中仅包含:

<?php echo var_dump($_GET); ?>

我的 htaccess 看起来像这样:

RewriteEngine on
#Alternate default index page
DirectoryIndex home.php
# To set your custom php.ini, add the following line to this file:
# suphp_configpath /home/yourusername/path/to/php.ini
# php_value mbstring.func_overload 4 # Required for PWSB support. Please do not uncomment this line.


RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html [L]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^motionpicturepicker\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.motionpicturepicker\.com$
RewriteRule ^/?$ "http\:\/\/furff\.com" [R=301,L]

据我所知,这也删除了 .php 和 .html 扩展名以及 www。

现在我已经尝试了几乎所有谷歌必须提供的干净 url 示例(大约 20 种),但没有一个完全可以正常工作。

添加这是我最接近的:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?film=([^\s&]+) [NC]

RewriteRule ^ index/%1? [R=301,L]
RewriteRule ^index/([^/]+)/?$ index.php?film=$1 [L,QSA]

这使得去index.php?film=whatever重定向到/index/whatever 不过/index/whatever返回的是Error 500 - Internal Server Error而不是index.php。 但是,如果我手动将 url 更改为 index.php/whatever 它确实有效,但返回一个空数组。

那么我怎样才能让干净的 url 加载页面和干净的字符串查询也能正常工作呢?

【问题讨论】:

    标签: php html apache .htaccess mod-rewrite


    【解决方案1】:

    试试这些规则:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(www\.)?motionpicturepicker\.com$ [NC]
    RewriteRule (.*) http://furff.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule (.*) http://%1/$1 [R=301,L]
    
    RewriteCond %{THE_REQUEST} \s/+index\.php\?film=([^\s&]+) [NC]
    RewriteRule ^ index/%1? [R=301,L]
    
    RewriteRule ^index/([^/]+)/?$ index.php?film=$1 [NC,L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*)$ $1.html [L]
    

    【讨论】:

    • 完美,谢谢!我所拥有的有什么问题?顺序?
    • 给您带来问题的主要是您的规则排序。
    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多