htaccess简单重写规则不起作用

时间:2015-02-10 00:01:31

标签: apache .htaccess mod-rewrite url-rewriting

我从来没有做过重写规则,我尝试在Google上找到的所有内容都无法正常工作。特别是创建多个重写规则对我来说很难,因为我不知道正确的语法是什么以及正确的实现如何(1重写条件或多个类似的问题)。

因此,我很乐意为以下尝试获得结果:

https://www.domain.com/our_accounts.php - > https://www.domain.com/accounts

http - > https规则已经有效。也许我的重写规则也存在问题,因为我可能需要在我的https规则之前添加它们?我希望你们可以帮助我。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Rules for readable URLs
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index.php$ /$1 [R=301,L]
    RewriteRule ^accounts$ /our_accounts.php [L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

以这种方式试用你的htaccess文件。我还将HTTP和www浓缩为一个重写器。只需将yoursite.com替换为您的真实网站即可。

<IfModule mod_rewrite.c>
    RewriteEngine On
     # First rewrite to HTTPS and redirect www:
    RewriteCond %{HTTPS} !^on [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]

    RewriteRule ^our_accounts.php$ /accounts [R=301,L]

    # Rules for readable URLs
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index.php$ /$1 [R=301,L]
</IfModule>