htaccess of virtual subdomain with subfolders - .htaccess

currently i have achieved
www.username.domain.com to redirect to domain.com/folder1/folder2/index.php?id=username
with my current .htacceess
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^www\.(.*)\.domain\.com$
RewriteRule ^$ http://domain.com/general/user/index.php?id=%1 [P,L]
i want to achieve this
www.username.domain.com/pagename/ to redirect to domain.com/folder1/folder2/next.php?id=username
www.username.domain.com/pagename/variable to redirect to domain.com/folder1/folder2/next.php?id=username$val=variable
Please help

Use this rule (place after yours):
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^www\.(.*)\.domain\.com$
RewriteRule ^pagename/([^/]*)$ http://domain.com/folder1/folder2/next.php?id=%1&val=$1 [P,L]
This will work for both www.username.domain.com/pagename/ and www.username.domain.com/pagename/variable (for 1st URL value of val= will be empty string).
P.S.
I assume $ character is a typo in your 2nd destination URL (domain.com/folder1/folder2/next.php?id=username$val=variable) and it should be & instead.

Related

Redirect subdomain to different locations based on url

I have a website on a domain that I want to move to another domain. Here is my problem. The root URL has to redirect to a different location on the new domain. Example:
http://subdomain.olddomain.com/ must redirect to http://www.newdomain.com/location
http://subdomain.olddomain.com/code/slug must redirect to http://www.newdomain.com/slug
The htaccess code that I have doesn't work ver
RewriteRule ^/?(.*) http://www.newdomain.com/$1 [R=302,L]
This code works for the second example, but nog on the first. Can anyone help me setup the correct htaccess code?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^code/(.*)$ http://www.newdomain.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^$ http://www.newdomain.com/location [L,R]

Rewrite Conditions in htaccess file

RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteCond %{REQUEST_URI} ^/ex-ex[/]*
RewriteRule ^(.*)$ /subsite/ex [R=permanent,L,NE]
I am redirecting mysite.com/ex-ex to mysite.com/subsite/ex
This is working fine, but mysite.com/ex-ex/page1 redirects to mysite.com/subsite/ex. How can I redirect to mysite.com/subsite/ex/page1?
How can I append the remaining part of url also to new url pattern?
This would do it:
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/ex-ex(.*)$ /subsite/ex$1 [R=permanent,L,NE]
Edit: added a slash in the Rule

htaccess issue redirecting certain urls

I have a bunch of .htaccess rules that follow this format
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
Now I need to put in a new rule to include a category string in my URL and was given this code
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
But it's not being fired for some reason, can someone please show me how to change the above string to match the rule further up.. so I check for some condition before executing the rule?
I only need this rule to fire if the url is in the format http://www.lazygamer.net/post-name/ and not when the url is in the format http://www.lazygamer.net/category/post-name/
RewriteCond %{REQUEST_URI} !^/category/.*$
RewriteRule ^([^/]+)/ /category/$1/ [R]
My htaccess file now looks like this and solves the problem
RewriteCond %{HTTP_HOST} !^images.lazygamer\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/(wp-content|wp-admin|wp-includes|fixed|contact-details|advertise|about|category|submission|images|ps3|xbox-360|wii|other-news|video).*$
RewriteCond %{REQUEST_URI} !^\/$
RewriteRule ^(.*)$ http://www.lazygamer.net/fixed/$1 [R=301,L]
The second last line ignore the root folder which kept me up all night...

.htaccess redirect from subdomain's articles to main domain

I have subdomains: sub1.tld.com, sub2.tld.com and so on. This subdomains contain important info. But I want to prevent opening any other urls, for example: sub1.tld.com/anypage or sub2.tld.com/dir/more/anyurl.html. This urls must be redirected to main domain tld.com: tld.com/anypage or tld.com/dir/more/anyurl.html
So, i have to check 2 things before redirection:
it's any subdomain
it's not subdomain's root page
I created this rules:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://tld.com/$1 [R=301,L]
This set of rules works as expected (redirects sub1.tld.com/anypage to tld.com/anypage) but also redirects sub1.tld.com to tld.com. Where is the error? 2nd string is wrong?
Try this:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$ [NC]
RewriteRule ^/(.+)$ http://tld.com/$1 [R=301,L]
Or you can use this string to check if it root:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /\ HTTP/
For some unknown reason the most reliable for checking variable was %{THE_REQUEST}, not %{REQUEST_URI}.
%{THE_REQUEST} contains raw browser's query to web-server: for example, GET /anypage.html HTTP/1.1, if you are opening url http://anysite.com/anypage.html.
So, here you are complete working rule-set for .htaccess:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$
RewriteCond %{THE_REQUEST} ^get\ /(.+)\ http.* [NC]
RewriteRule ^(.+) http://tld.com/$1 [R=301,L]

Simple htaccess redirect - subdomain to folder

I am trying to redirect http://[random-string].domain.com/ to http://domain.com/folder/[random-string]/
I currently have this:
RewriteCond %{HTTP_HOST} ^.*\.domain\.com
RewriteRule ^.*$ http://domain.com/folder/$1/ [R=301,L]
It currently points to http://domain.com/folder//. (The $1 is missing) How do I fix this?
You need to use parenthesis to grab the value matched, in your case:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/folder/%1/$1 [R=301,L]
assuming you also want to redirect http://[random-string].domain.com/something to http://domain.com/folder/[random-string]/something
how i fix it?
my subdomain not is randomically
have where the replace for $1 ?
cheers
RewriteCond %{HTTP_HOST} ^(.).domain.com
RewriteRule ^(.)$ http://domain.com/folder/%1/$1 [R=301,L]

Resources