简体   繁体   中英

Rewrite problems with mod_rewrite and Codeigniter

This question might look intimidating. No, it's simple but thoroughly explained. So please keep on reading.

I need to make a multilingual page (English and Spanish) and I want to use this segment to route each language:

mysite/ en /mycontroller/mymethod

These are the rules I made in .htaccess and their description:

# Rewrite url removing language segment and appending it as a request variable
RewriteRule ^(es|en)/(.*) $2?lang=$1 [L]

# Remove index.php from URL
RewriteCond $1 !^(index.php|css|images|scripts|robots.txt|sitemap.xml) 
RewriteRule ^(.*)$ index.php/$1 [L]

There is something I'm not doing correctly:

The 1st rule is creating the request variable lang correctly, but the URL is still being routed without removing the language segment ( en|es ), which causes an error because Codeigniter assumes (so I think) to expect a controller called "en" or "es" . I could eventually get rid of this segment in the CI routes, but since I'm doing it in .htaccess there is no need to repeat it again. As far as I know, this segment shouldn't exist anymore after the rewrite.

Note : I don't want to use CI routes for this particular case because of these reasons:

  • They don't work by default with appended request variables,
  • I don't want to use visible request variables in public URL's (no way enabling them in config file),
  • Passing the request variable as a parameter segment would mean the need to retrieve it in the index or whatever else requested method,
  • I want to process this before, within the constructor (retrieving $_GET('lang') in it).

Just in case this helps, these are the $_GET and $_SERVER variables (not all) dumped just when entering index.php:

Example URL: http://localhost/mysite/en/portfolio

$_GET:

Array
(
    [lang] => en
)

$_SERVER:

Array
(
    [DOCUMENT_ROOT] => C:/xampp/htdocs
    [CONTEXT_DOCUMENT_ROOT] => C:/xampp/htdocs
    [SCRIPT_FILENAME] => C:/xampp/htdocs/mysite/index.php
    [REDIRECT_QUERY_STRING] => lang=en
    [REDIRECT_URL] => /mysite/portfolio
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => lang=en
    [REQUEST_URI] => /mysite/en/portfolio
    [SCRIPT_NAME] => /mysite/index.php
    [PATH_INFO] => /portfolio
    [PATH_TRANSLATED] => C:\xampp\htdocs\portfolio
    [PHP_SELF] => /mysite/index.php/portfolio
)

The same URL without the language segment works perfectly and enters the Portfolio controller: http://localhost/mysite/portfolio

Any help would be most welcome.

Try this code once.........

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

尝试将RewriteRule ^(es|en)/(.*) $2?lang=$1 [L]更改为RewriteRule ^(es|en)/(.*) index.php/$2?lang=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM