开发者

How to rewrite URLS to remove index.php AND force lowercase with codeigniter?

I’m having trouble doing both of these at once with .htaccess.

Removing index.php is working, but trying to add the force lowercase keeps throwing a 500 error. I am using codeigniter. Any help is appreciated!

here’s my .htaccess code:

Rew开发者_JAVA技巧riteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]


The php code suggested does not work if you are just trying to force to lowercase. Below is the correct php code.

$url = $_SERVER['REQUEST_URI'];
$pattern = '/([A-Z]+)/';

if(preg_match($pattern, $url)) {
    $new_url = strtolower($url);

    Header( 'HTTP/1.1 301 Moved Permanently' );
    Header( 'Location: ' . $new_url );
    exit;
}

// your code here

Discussion Found Here Force lowercase .htaccess


That is because it is impossible, let me explain.

  1. Removing the index.php still sends all requests to index.php/yadda/yadda so the requested uri still has the index.php in it even though you do not see it or add it into the url.
  2. The redirect (triggered by the 2nd rewrite rule) nulls the index section so you would then get mysite.com/index.php/lowercase/

BUT beyond all this RewriteMap can only be declared in your Httpd.conf file, you would declare it there with:

RewriteMap  lc int:tolower

Then in your .htaccess file use your variable lc, but then again only one of the two will win you can't have both.

You will either get the URL lowercase or have the site working without using the index.php because they are always going to conflict because of the nature of what each one does.

Really the only way I can see it happening is in php, like so:

$this->load->helper('url');
$your_URL = uri_string();
preg_match_all('/[A-Z]/', $your_URL, $match) ;
$total_count = count($match [0]);
if($total_count > 0){
    $new_line = strtolower($your_URL);
    redirect($new_line);
}

This I would put into the main construct of your classes, I hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜