/index.php/, /index.php?/, /?/ in codeigniter urls - apache

Although we've redirected index.php to root, still we are getting inner page URLs in following 4 versions:
https://example.com/articles [/index.php/articles, /index.php?/articles & /?/articles]
Would anyone explain why?
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{http_host} ^www.example.com
RewriteRule ^(.*) example.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|phpinfo\.php|Charts|assets|uploads|userfiles|c‌​ustomer-uploads|appl‌​ication|sitemap\.xml‌​|sitemap\.html|robot‌​s\.txt|gyan\.rss)
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

Try using this simpler method
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php| (.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

Related

.htaccess issue with rerouting to https

I have this in my .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
RewriteCond %{HTTP_HOST} ^www\.clipboards\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
These condition are working fine with one exception.
If I come from http (non-ssl) it will add index.php?route to my url before sending it to https. This is not the case if I come directly from https in which case it works fine. Can someone help me with this. thanks
You should put your redirects at the top and you can combine the bottom 2 rules. Also note that you have invalid syntax [R,L=301], it should be [R=301,L]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

.htaccess rewrites a little sluggish

I have the following .htaccess code that tells the server to point certain sub domains to to a sub folder and then also handles the rewriting of the root domains.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 ./404.php
RewriteCond %{HTTP_HOST} ^secretplace\.testwebsite\.local
RewriteRule ^(.*)$ /secretplace/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^payment\.testwebsite\.local
RewriteRule ^(.*)$ /payment/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^login\.testwebsite\.local
RewriteRule ^(.*)$ /login/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^signup\.testwebsite\.local
RewriteRule ^(.*)$ /signup/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^fbauth\.testwebsite\.local
RewriteRule ^(.*)$ /fbauth/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^masterapi\.testwebsite\.local
RewriteRule ^(.*)$ /masterapi/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.testwebsite\.local$ [NC]
RewriteRule ^(.*)$ http://testwebsite.local/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^testwebsite\.local$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ api.php?function=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ $1-$2.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
</IfModule>
It works, but on my local apache install the page loading is real sluggish. It takes a few seconds just to switch between pages.
Just so you know the very last line of code allows me to remove the .php extension from pages. So instead of http://testwebsite.local/features.php, it loads http://testwebsite.local/features/.
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
Is there something obvious in my code that I'm doing wrong? I'm not quite sure how to debug .htaccess.
Why are you using P flag instead of L flag ? This requires mod_proxy and can slow down htaccess execution. Seems to me you don't need it in your case.
If i had to write your code myself, it would look like this
ErrorDocument 404 /404.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(secretplace|payment|login|signup|fbauth|masterapi)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ /index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ /api.php?function=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /$1-$2.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteRule ^.*$ /%1.php [L,QSA]

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

redirect all non www to www. for Cakephp site

Hi allm i'm having some issues in redirecting the non www pages to the www. pages for my cakephp site.
I've tried
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
but it doesn't seem to work.
My cakephp app resides in a subfolder. eg. www.domain.com/my.
i've added the above code to the root folder, www.domain.com/
any suggestions? thanks?
*update
This my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L]
i'm getting an extra webroot/index.php?url= inserted into the url. Instead of www.domain.com/my/apple, i'm getting www.domain.com/my/webroot/index.php?url=apple
thanks to all once again.
Your code is not quite right. See below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L] # <-- Mind the 'L'!
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
if domain is .co.uk
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.domain.co.uk%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app/webroot/$1 [QSA,L]
I have tried the below code below and it worked for me:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gigsadda\.com$ [NC]
RewriteRule ^.*$ http://www.gigsadda.com%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

.htaccess Apache URL Rewrite for Wordpress - permanently pointing one domain to another

I am trying to point www.olddomain.com/whatever to www.newdomain.com/whatever (as well as without the www.), but the Wordpress permalinks are not staying intact. Please help!!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]
</IfModule>
I fixed it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc,L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
A much simpler solution. Change the .htaccess to just say:
RedirectMatch 301 /(.*) http://www.newdomain.com/$1
You will need to move this block:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
behind the redirects. It grabs everything and sends it to index.php.