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

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).

Related

Redirect 301 appending old URL with New URL

I am having a problem while redirecting a page whose URL was changed but I want old URL to be redirected to new. Here is .htaccess file
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Using mod_rewrite in the last line I am not getting redirected to new URL but when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended to in it in this format.
https://example.com/contact-us/?/contactus/
I have searched Stack Overflow for solution to this problem and tried many similar scenarios but that didn't work.
Have it like this the given order.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^contactus/$ /contact-us/ [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/system [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On is not required multiple times in same .htaccess.
Make sure to clear your browser cache before testing the change.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Your mod_rewrite external redirects (the last three rules) are in the wrong place. These need to be before the internal rewrite (front-controller). By placing them at the end of the file they are never going to get processed for any URL other than static resources, since all other URLs are routed to index.php, at which point processing stops.
when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended
Different modules are processed independently regardless of the order of the directives in the config file. mod_alias is processed after mod_rewrite - but it is processed. However, the Redirect directive is prefix matching and everything after the match is appended onto the end of the target URL.
And there's no need to repeat the RewriteEngine directives
Have your directives like this:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/system
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
And make sure to clear your browser cache before testing. Preferably test with 302 (temporary) redirect to avoid caching issues.

How do I edit apache .htaccess correctly?

Good day! There are 3 tasks that I want to implement using the .htaccess file:
Work only with https protocol
Create a 301 redirect from https://www.example.com to https://example.com
Redirect all requests to the index.php file
My best version of .htaccess looks like this, but I'm not sure if this is the right decision:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} .* [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
I would be grateful for any advice!
Could you please try following, written as per your shown samples. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
##For applying https on each request.
RewriteCond https !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##For removing www from request.
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
##For applying index.php for non existing files or directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Remove .php extension, force non-slash, force non-www, force https with .htaccess

I have been looking into this for quite a while now, but i just can't get it right. When I have one part working (e.g. forcing non-php), the other part stops working (e.g. forcing non-slash).
What I want exactly:
remove .php extension (so example.com/foo.php becomes example.com/foo in the browser url bar)
remove the slash at the end of a URL (so example.com/foo.php/ becomes example.com/foo. BUT when it's a directory with an index.php inside, it has to redirect accordingly (i can't get this last part to work)
force non-www (this works)
force https (this works)
What I my .htaccess looks like right now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I experience the following problems with this:
example.com/foo.php/ redirects to example.com/foo.php instead of example.com/foo
example.com/foo.php/foo displays the example.com/foo.php file, while example.com/foo/foo displays a 404 message (the latter would be correct in my case)
example.com/admin (this is a folder with an index.php in it) displays a 404 message
I appreciate your help!
Okay, so I know it is a broad question. But if anyone ever searches for this, I almost have it (the only thing that doesn't work is redirecting domain.com/foo.php/foo to domain.com/foo/foo
Here is the .htaccess file I have right now:
RewriteEngine On
Options -Indexes
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
RewriteRule ^ /%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

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.

.htaccess redirect trouble

I'm having trouble with (what should be) a simple .htaccess redirect. I need to changed any url that has "for_homes" into "for_home".
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
However when I go to any pages with for_homes I get a 404.
Rewrite Rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
</IfModule>
The problem is a preceeding rule is rewriting to index.php. In this block here:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### This rule rewrites "for_homes" to "index.php?/for_homes"
RewriteRule ^(.*)$ index.php?/$1 [L]
### These rules never get applied
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
So perhaps after the RewriteCond %{REQUEST_FILENAME} !-d, add conditions to exclude the later 2 rewrites, So that it looks like this:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/for_home(s)?
RewriteCond %{REQUEST_URI} !^/for_business(es)?
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
If it's your intention to have index.php handle for_home and for_businesses, so that when someone enters http://domain.com/for_homes/stuff in their browser's address bar, the browser gets redirected to http://domain.com/for_home/stuff, then it gets internally rewritten to /index.php?/for_home/stuff so that index.php can handle the request, you simply need to move the 301 redirects before the index.php rewrite:
### Redirect first
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Perhaps you are just missing RewriteEngine On
RewriteEngine On
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]