How to remove index.php from htaccess when redirect 301 - Codeigniter - apache

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.

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.

Related

How to remove index.php from www to non-www .htaccess redirects

When I go to
www.example.co/courses
I get redirected to
https://example.co/index.php/courses
I want to get redirected to
https://example.co/courses
What I have tried
Here is my .htaccess file at the moment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301]
</IfModule>
How do I remove the /index.php/ part of the redirected URL?
You've got your external redirects in the wrong place. They need to go before your internal rewrites (before your front controller). For example:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.twocan\.co [NC]
RewriteRule ^(.*)$ https://twocan.co/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have your external redirects after the internal rewrite then the request gets rewritten to /index.php/... which then gets turned into an external redirect (the rewrite process loops in .htaccess, so it only gets rewritten to index.php on the first loop).

Using htaccess to redirect all pages but one to another domain

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Merge addition of URL rewriting of sub-domain and single page to capture all URLs

I have presently setup .htaccess according to the answer given here to capture all URLs in a single file. Now I want to capture all URLs having no "www" subdomain and redirect to www.my-site.com.
So, presently I have (to capture all URLs in a single file):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
And I want to merge the following with it (capture all URLs having no "www" subdomain and redirect to www.my-site.com):
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
First the redirection, and after the rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

.htaccess redirect behaviour

I have a .htaccess file which redirects the user to https:// and adds /de/index if nothing else is specified.
RewriteEngine on
RewriteRule ^(hotelaccess)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/de/index [L]
RewriteCond %{HTTP_HOST} !^www\.mypage\.de
RewriteRule (.*) http://www.mypage.de/de/index [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]
but now if I enter https://www.mypage.de it get's redirected to https://www.mypage.de/index ? Why is that and what do I have to add to fix this?
Clarification: The default URL should always be https://www.mypage.de/de/index if entering with and without www and both http:// and https:// (also with or without www)
You need to put your redirects first, then your rewrites. You can also combine the two redirects into a single rule:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} ^mypage\.de$ [NC]
RewriteRule ^(.*)$ https://www.mypage.de/$1 [R,L]
# for no URI
RewriteRule ^$ /de/index [L]
# for hotelaccess
RewriteRule ^hotelaccess - [L]
# for everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]

mod_rewrite: HTTPS for only one page

I want to redirect to HTTPS only /login page. Rest of pages needs to be HTTP.
.htaccess file looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/login
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
I don't know why it doesn't work. When I go to /login I was redirected to example.com/index.php
What is wrong?
Is there an actual (physical) /login folder or file in your directory? I suppose not, so your /login request is finally captured by the last rewrite rule and redirected to index.php.
Try adding this RewriteCond %{REQUEST_URI} !/login above this line:RewriteCond %{REQUEST_FILENAME} !-f ( or just handle the login request differently )
Try adding L flag in your last rule have your code like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/login [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]