1

I have a problem in Codeigniter when the url type is:

example.gov.ar/news (without www and .gov)

I need it to redirect to:

www.example.gob.ar/news (with www and .gob)

But it redirect

www.example.gob.ar/index.php/news

This is my htaccess file:

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

#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$ 
RewriteRule ^(.*)$ http://www.example.gob.ar/$1 [R=301,L]

Note: my config file have $config['index_page'] = '';

Thanks in advance.

1 Answer 1

2

Try switching the order of your rules.

RewriteEngine on

#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$ 
RewriteRule ^(.*)$ http://www.example.gob.ar/$1 [R=301,L]

# Redirect to index.php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

An external redirect triggered by .htaccess is re-evaluated against all rules, therefore order is important.

In the changed order, the external redirects happen first, so just the final URL is matched against the third RewriteRule, which then does not become visible externally.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! Genius!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.