不能让htaccess使用我的所有命令

时间:2018-01-18 17:12:53

标签: php .htaccess

这就是我需要发生的事情:

  1. 将站点主目录设置为“/ home”
  2. 对于数据库驱动的页面,转换“index.php?page =”(如果链接转到“/ about”,它会无形地将其转换为“index.php?page = about”)
  3. 对于非数据库驱动的页面,只需从链接中删除“.php”(“/about.php”转到“/ about”)
  4. 我在使用web.config进行ISS安装时工作正常,但我无法将其转换为.htaccess。这是我到目前为止所做的:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    #Alternate default index page
    DirectoryIndex index.php?page=home
    
    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.+) index.php?page=$1 [QSA,L]
    RewriteRule    "^/index\.php$"  "/index.php?page=home" [PT]
    
    RewriteRule ^/?([^/d]+)/?$ index.php?page=$1 [L,QSA]
    
    
    AddCharset UTF-8 .php
    ErrorDocument 404 /404.php 
    
    </IfModule>
    

    使用上面的htaccess代码,3)很好,但其他两个不起作用。

    任何人都可以帮助这个菜鸟吗?我在Godaddy主持,如果这有所作为。此外,此站点位于测试子子目录的深处,因此任何根指示都不起作用。

    更新:

    我可以通过将完整的URL添加到RewriteRule来获得SOMETHING -

    AddCharset UTF-8 .php
    RewriteEngine On
    
    #Alternate default index page
    RewriteRule ^home/?$ http://example.com/test/site/index.php?page=home [L,QSA,NC]
    RewriteRule ^/?$ http://example.com/test/site/home [L,QSA,NC]
    
    
    # add .php extension if corresponding file exists
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1 [L]
    
    # everything else
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.+) http://example.com/test/site/index.php?page=$1 [QSA,L]
    

    但现在它与我想要的相反;而不是去“example.com/test/site/about”,它重写它去“example.com/test/site/index.php?page=about”

    更新2:

    最终工作版本以防其他人遇到同样的问题:

    AddCharset UTF-8 .php
    RewriteEngine On
    RewriteBase /test/site/
    
    #Alternate default index page
    RewriteRule ^home/?$ index.php?page=home [L,QSA,NC]
    RewriteRule ^/?$ /home [L,QSA,NC]
    DirectoryIndex index.php?page=home
    
    
    # everything else
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.+) index.php?page=$1 [QSA,L]
    

1 个答案:

答案 0 :(得分:1)

这样做:

AddCharset UTF-8 .php
RewriteEngine On

#Alternate default index page
RewriteRule ^home/?$ index.php?page=home [L,QSA,NC]

# add .php extension if corresponding file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?page=$1 [QSA,L]
不需要

ErrorDocument,因为每个非文件,非dir都会转到index.php?page=...