issue in url rewrite in .htaccess - php

i am trying to force non-www to www and its working as aspected:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
now if i visit www.example.com/registration it works great.. but when i am accessing the same url from an link it works but adds index.php?/ in the url like this:
www.example.com/index.php?/registration
i am new to .htaccess programming.. i can understand i need ot modify the below lines.. but i dont know how to modify in such a way so that i does not add index.php?/ in the url...
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
any help or suggestion would be great help.. thanks in advance

Swap the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Related

Getting value with htaccess and pagination for root

This url
example.com/videos/load.php?cat=video-category
would become
example.com/videos/video-category
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&p=2&limit=20
it would become
example.com/videos/video-category?p=2&limit=20
(this links also work)
example.com/videos/video-category?p=2
example.com/videos/video-category?limit=20
Also, there is another root url
example.com/videos/load.php?cat=video-category&name=video-name
it would become
example.com/videos/video-category/video-name
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&name=video-name&p=2&limit=20
And it would become
example.com/videos/video-category/video-name?p=2&limit=20
(this links also work)
example.com/videos/video-category/video-name?p=2
example.com/videos/video-category/video-name?limit=20
Here is my htaccess (into videos folder)
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \ /+videos/load\.php\?cat=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ %1?%2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]
how to do this.. plz help me.. thanks..
Have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&name=([^&\s]+)&?(\S*)
RewriteRule ^ %1/%2?%3 [L,R=302]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&?(\S*)
RewriteRule ^ %1?%2 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ load.php?cat=$1&name=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]

htaccess - add www & remove index.php

I have my current htaccess as:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
when I go to to abc.com, it redirects properly to www.abc.com which is good.
However, if I go to abc.com/about - it redirects to abc.com/index.php?load=about, I want it to redirect to www.abc.com/about.
Please help.
Change order of rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?load=$1 [PT,L,QSA]
To force WWW:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
To remove index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

htaccess rewrite rule to index.php?p=pagename

I am using this htaccess code to create a rewrite rule
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ index.php?p=$1.php [L,QSA]
i want to rewrite pages like
http://my.domain.net/index.php?p=tickets/openticket to look like http://my.domain.net/tickets/openticket
but its just showing index.php without the ?p=...
Try this code as your first rules:
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
try this.

working with symbolic link before the controller/method in codeigniter

i just needed some help i think its about time to ask some good felas for help.
here's my situation, i've been trying to use a symbolic link to fake my url just like this.
www.website.com/uk/controllers/method/etc
www.website.com/us/controllers/method/etc
aparently codeigniter failes to read the controller. the uk and us segment is a symlink to root directory so I expect it should point that to root. what made me sure that it points to the root is by viewing www.website.com/uk/ it works fine no erros all good. please help me on how to fix this. it might be i'm missing something like in htaccess or in route file. i've been having a headache about this for a day.
here's my current htaccess contents
Options FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I'm not sure if this will help but if you are using language class, then have a look at this extention:
https://github.com/EllisLab/CodeIgniter/wiki/URI-Language-Identifier
Try this code for redirecting /us/ and /uk/ to root:
Options FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_URI} ^(system|application)
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^(?:us|uk)/(.*)$ /$1 [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Configure mod_rewrite to add "www" and to support MVC

I would like to add www into the URL if the users forgot to type and still supporting MVC by redirecting to the index.php with [QSA]
This is my current working .htaccess
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]
Using this .htaccess everything works fine, I can type www.abc.com/xxx/yyy to visit any specific pages
Anyway because I'm using Facebook authentication so I must prevent my users from typing the site URL without www, so I tried to edit my .htaccess
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [R=301,QSA]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]
With this configuration if I typed abc.com I was redirected to www.abc.com, but if I typed abc.com/xxx/yyy I was only redirected as www.abc.com instead of being redirected to the specific page.
I also tried
# 1
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [QSA]
# 2
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/index.php [QSA]
# 3
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [L,QSA]
None of them is working, please guide what to do
Thanks.
Add this 2 lines above your .htaccess file
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
so your final htaccess will look like this
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

Categories