.htaccess问题将www添加到url并删除index.php

时间:2011-05-09 17:11:08

标签: apache .htaccess mod-rewrite

我不确定为什么我的代码不起作用。 我想在网址中添加www,当他们输入 http://domain.co.uk/index.php 时 它只会变成 http://www.domain.co.uk/ 这是我的htaccess代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteRule index\.php http://www.domain.co.uk/ [NC,R]
</IfModule>

2 个答案:

答案 0 :(得分:1)

.htaccess文件中的一些小修正:

RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]

RewriteRule ^index\.php/?$ http://www.domain.co.uk/ [NC,R,L]
  • .需要在条件下进行转义,否则会匹配任何字符
  • 通过放置L
  • 将每个单独的规则标记为最后一个
  • ^$中附加条件以标记比赛的开始和结束

答案 1 :(得分:0)

如果有人仍需要答案。使用另一个.htaccess。从这里获取指南,我找到它并且看起来不错:http://www.farinspace.com/codeigniter-htaccess-file/

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Canonicalize codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

    ###

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

请记住,一旦设置了CodeIgniter htaccess文件,您将需要进入“/system/application/config/config.php”,找到以下内容:

$config['index_page'] = "index.php";

并将其更改为:

$config['index_page'] = "";