htaccess for codeIgniter - .htaccess

I'm trying to redirect a user to /hd/142 if they don't have /hd/ in the URL. I've tried so many options and most of them just redirected me over and over again. Here is the current .htaccess that I have setup.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js)
RewriteRule ^hd/(.*)$ /index.php/$1 [L]
RewriteRule ^(images|css|js)/(.*) /applicationFiles/$1/$2 [L]
Thanks

RewriteEngine on
RewriteBase /
#if url does not have hd in it
RewriteCond %{REQUEST_URI} !^/hd/ [NC]
#redirect to hd
RewriteRule ^ /hd/142 [L,R=301]
#existing rules
RewriteCond $1 !^(index\.php|images|css|js) [NC]
RewriteRule ^hd/(.*)$ /index.php/$1 [L,NC]
RewriteRule ^(images|css|js)/(.*) /applicationFiles/$1/$2 [L]

A simple solution would be to use routes.php in codeigniter:
$route['^(hd)'] = '/hd/142';
Read more under the Regular Expressions section here: http://codeigniter.com/user_guide/general/routing.html

Related

Need to correct htaccess to restrict some folder

I am using following htaccess code in my project
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
#RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^ /%1.html [R=302,L,NE]
#RewriteRule ^(.+?)\.html$ $1.php [L,NC]
Now everything is working fine except one thing I have some webservice also that I am calling from webservice folder, url is
http://localhost/apnaujjain/webservice/gethomepagecontent1.php
Now the problem is due to htaccess my webservice stopped working, it redirecting to wrong page so its not working. I don't want to apply htaccess rule on webservice folder. Please help, thanks in advance.
Just below RewriteBase /apnaujjain line add this line:
RewriteEngine On
RewriteBase /apnaujjain/
# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]
# rest of your existing rules

.htaccess rewrite multiple parameters

I try to rewrite my URLs from domain.tld/?country=XX&admin1=YY&city=ZZ to domain.tld/XX/YY/ZZ
I have tried
RewriteEngine on
RewriteRule ^([A-Za-z]{2,2})/(.*)/(.*)$ /?country=$1&admin1=$2&city=$3 [QSA]
Unfortunately it doesn't work. Have I missed something?
You can have these rules in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?country=([^\s&]+)&admin1=([^\s&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ /?country=$1&admin1=$2&city=$3 [QSA,L]

.htaccess RewriteRule subsubdomain.subdomain.domain.tld to subdomain.domain.tld/subsubdomain

Is it posible to redirect a sub-subdomain into a subdomain with the sub-sub domain as new folder using .htaccess rewrite-rule?
For example... When i go to 2013.archive.example.com I want to end up in archive.example.com/2013..
Already tried some things, current .htaccess is:
RewriteRule On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$
RewriteRule ^(.*)$ archive.example.com/%1/$1 [L]
Unfortunately it isn't working. Any suggestions for this?
Changed it a but, currently using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
and is working exactly how I wanted:)
For external redirect: (URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
This will work provided DOCUMENT_ROOT is same for anything.archive.example.com and archive.example.com.
For internal forward: (No URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.archive\.example\.com$ [NC]
RewriteRule ^ /%1%{REQUEST_URI} [L]
If they share the same root, then you don't need the archive.example.com part in your rule's target:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Replacing link with .htaccess

I am trying to replace link but ending with internal server error
www.domain.com/profile.php?id=1
i want it to ->
www.domain.com/1
.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^login\.php\/?(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
You just have to use this code :
RewriteEngine on
RewriteRule ^([0-9]+)$ /profile.php?id=$1 [L]

RewriteRule at htaccess wrong redirect

ive this rules at htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
issue happend when redirect warcraft its redirect to
http://www.site.com/home/site/public_html/www.site.com/forumdisplay.php?f=480
any tip ?
I suppose you want to redirect it to http://www.site.com/forumdisplay.php?f=480
If so put it in your .htaccess file:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ http://www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
You must provide the full URL including protocol in your rule, in this case http://www.site.com/.....

Resources