Rewrite htaccess to point to a folder - php

The below is original .htaccess, i want to modify it to point to a different folder such as www.yourdomain.com/clients/foldername. How do i do it?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
do let me know what i should do that it point to that particular folder. Should i write like this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.yourdomain(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
do help me

This should take care of it.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ your/new/directory/$1 [R=301,L]
This will match all requests. If you only want to redirect certain folders to another you will need to change the RegEx on the RewriteRule.
RewriteRule ^some/path/to/redirect(rest/of/the/uri)$ your/new/directory$1 [R=301,L]
To redirect to a different domain use a RewriteRule like this.
RewriteRule ^(.*)$ http://the.newdomain.tld/your/new/directory/$1 [R=301,L]

Related

How to Force domain to with www - htaccess

I'm using this htaccess to force all request with www. but my resources don't loaded:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteRule ^ /index.html [L]
this is my site:
http://www.shadyab.com/
for example:
http://www.shadyab.com/assets/plugin/slider/css/owl.carousel.min.css
Your second rule rewrites everything to index.html, including all your css
If you really want to rewrite every request to index.html, but still want your resources, you can exclude them using a condition, otherwise remove the rule.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpe?g|gif)$ [NC]
RewriteRule ^ /index.html [L]

Htaccess multiple redirect rules should be working

This should be working according to htaccess tester http://htaccess.madewithlove.be/
I'm trying to get url's in the format subdomain.domain.com to resolve to domain.com/index.php?sub=subdomain
However I've also want any links in the form subdomain.domain.com/pagename to redirect to domain.com/index.php?tpl=page&sub=subdomain&url=pagename
At the moment the first rule works if i remove the second rule but if I include both only the second rule works.
Here's the full htaccess
RewriteEngine On
#EDIT: this was messing it all up by appending index.html so the subdomain only
# wasn't triggering at all due to appended pagename
DirectoryIndex disabled
#Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.+/)?([^/]*)$ http://example.com/index.php?tpl=page&sub=%1&url=$2 [P,NC,QSA,L]
If you answer this I will make you Secretary of State once Flat Earth goes mainstream. Thanks!
Have it like this:
DirectoryIndex disabled
RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ index.php [L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^index\.php$ http://%2/index.php?sub=%1 [P,QSA,NC,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(?:.+/)?([^/]+)/?$ http://%2/index.php?tpl=page&sub=%1&url=$1 [P,QSA,L]

redirect folder subdomain to subdomain.domain.com in htaccess

I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.

htaccess in subdirectory for parked domain

Motive: What I am trying to do is use parked domain and redirect it to subdirectory. and in subdirectory I have a script that needs pretty url's and htaccess is the only thing I can use.
Code in main .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^info/(.+)$ contact.php?a=$1
RewriteRule ^list/(.+)/page/(.+)$ list.php?a=$1&p=$2 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/m/$1 [R=301]
Code in /m .htaceess
RewriteEngine On
RewriteBase /
RewriteRule ^how/(.+)$ how.php?a=$1
RewriteCond %{HTTP_HOST} ^(www.)?main-example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/m/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
What i want is to achieve when user visits www.example.com/m/how/love the script should treet a get variable of a something like www.example.com/m/how.php?a=love but url should
remain pretty.
What my current code does is unlimited redirects to /m/404.shtml giving 301 in firebug.

Modify .htaccess to make URL?variable=true change to URL/variable?

I'm trying to change my .htaccess file so that if I go to:
http://www.example.com/index.php?login=true, it goes to http://www.example.com/login.
I currently have this code which removes index.php (which makes the above looks like http://www.example.com/?login=true).
RewriteEngine On
#remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET
I would setup your rewrite rule the other way around:
RewriteEngine On
RewriteRule ^login$ /index.php?login=true
This way if a user browses to http://yourserver.com/login the actual page used is http://yourserver.com/index.php?login=true, but the first URL is shown in the browser. I assume this is what you are trying to achieve.
If you really need to do it in the direction you asked for, you can try something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^login=true$
RewriteRule ^index\.php$ /login [L,R=301]
This will fail of there are additional query parameters.
If you want to redirect http://yourserver.com/index.php to http://yourserver.com you can simply add the following rewrite rule:
RewriteRule ^index\.php$ / [L,R=301]
The following should work although there might something wrong with the line to exclude /system/*. Try testing with that commented out.
RewriteEngine On
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} ^/system/.*
RewriteRule index.php /%1? [R=301,L]
The following page helped: http://wiki.apache.org/httpd/RewriteQueryString
This tester is cool but does have a few bugs in it: http://martinmelin.se/rewrite-rule-tester/
try the following in the .htaccess in root directory of example.com
RewriteEngine On
RewriteBase /
#rewrite http://www.example.com/anything to http://www.example.com/index.php?anything=true
RewriteCond %{REQUEST_URI} ^/([-a-zA-Z0-9]+)$ [NC]
RewriteCond %1 !system [NC]
RewriteRule . index.php?%1=true [L]
#301 redirect requests for example.com/index.php?anything=true to example.com/anything
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]+)=true$ [NC]
RewriteRule . /%1? [L,R=301]
You need to check for the QUERY_STRING in a RewriteCond. I think this should do:
RewriteEngine On
#remove index.php
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^index\.php /%1? [R=301,L]
This should redirect anything which has index.php?=true to /action

Categories