How do i redirect a url to domain .
eg. http://www.mydomain.com/index.php=HairThing --> http://www.mydomain.com
How do i redirect a non-www to www WITHOUT a slash at end ?
eg http://mydomain.com ---> http://www.mydomain.com
See also: Hidden features of mod_rewrite
#1
RewriteRule /index.php=HairThing$ http://www.mydomain.com [R=301]
#2
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule .* http://www.mydomain.com [R=301]
However, example case 1, as said by Greg, will always put the / on if it is without a uri.
mydomain.com # impossible
mydomain.com/ # possible
mydomain.com/foo #possible
mydomain.com/foo/ #possible
For your second question, the browser will always put a slash after the site name. This is because the trailing slash is required to indicate the root path of the web site.
you could use a general rule that works on every domain without having to change the name of the domain all the time. This is very helpful when you have multiple domains parked on same root.
RewriteCond %{HTTP_HOST} !^www\.[a-z0-9-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z0-9-]+\.[a-z]{2,6})$ [NC]
RewriteRule (.*) http://www.%1/$1 [L,R=301]
Related
I use rewrite condition to redirect website always to www. my code is:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
I can't found on the internet how can i do request if in url '.com' i need it because the website must be also localy accessible.
For example: i found this, but i can't understand how can i implement it with my script.
Your current RewriteCond is correct for applying www. to website.com if it is not already present. To avoid the RewriteRule happening when working on localhost, you need an additional RewriteCond to check the host.
This is because the condition !^www\.example\.com$ matches any domain except www.example.com, which includes localhost.
# Only apply other conditions if not working on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can make it a little more dynamic in case you need to work with multiple domains (example.com, example.org) and transform each to www.example.com, www.example.org:
# If not on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
# and the domain does not begin www.
RewriteCond %{HTTP_HOST} !^www\.
# Redirect to apply the www. to HTTP_HOST
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Use caution when testing - your browser may cache old 301 redirects. You may need to change them to R=302 during testing and clear your browser cache. When you are satisfied it works, change it back to R=301.
That RewriteCond should already work for your case, the next rule is processed only if the condition matches, so that rule is not processed for localhost.
I have a redirect working mostly but with one exception when a url is run without a forward slash the redirect shows in the address bar, this is not the behavior I'm expecting. Here is the .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) redirector.php?subdomain=%1 [QSA,L]
now if there is a subdomain on my domain I want that request to go to the redirector.php file and this works in all cases unless I have a folder without a backslash. I've also tried putting in an optional forward slash at the end of the condition, still no go. So what happens when there is no forward slash at the end of a folder is that the request goes from
http://test2.domain.com/test
to
http://test2.domain.com/test/?subdomain=test2
cannot figure out how to get it to stop as
http://test2.domain.com/test/
will work.
ah figured it out you can easily check to see if the request doesn't have a file name and if so redirect with a forward-slash! So now I have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) redirector.php?subdomain=%1 [QSA,L]
I am using two Domains which one of them is the Main Domain (http://www.domain.com) and the second is a Domain to shorten URL's (http://doma.in)
Forwards should be like this
http://domain.com -> http://www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
http:// doma.in -> http:// www. domain.com
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
http://sub.domain.com -> http://sub.domain.com (no configuration needed?!)
and now the important one
http://doma.in/VC7s98X -> should forward to its target without to be converted to http:// www.domain.com
I dont know how I can do this part.
Your second set of rules matches everything ((.*)) on the domain doma.in. To only match the bare domain without any trailing path, change the rule as follows:
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule ^/?$ http://www.domain.com/$1 [R=301,L]
That takes care of http://doma.in/VC7s98X not redirecting to http://www.domain.com. I'm assuming you have something in place to take care of http://doma.in/VC7s98X forwarding to its intended target ... if you're asking for help with that part, that's a much bigger question.
Struggling with mod_rewrite trying to redirect a non-secure page to a secure one. This works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://mysite.localhost/%1/ [R=301,L]
But this doesn't:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP:Host} (mysite.localhost|mylivesite.com)
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://%1/%2/ [R=301,L]
The URL it tries to give me is https://help/returns//
I can't seem to get the HTTP:host into the final RewriteRule line.
I need the host in there so I can use the same file for local dev and live deployment.
Most grateful for any input.
You can use this rule:
RewriteCond %{HTTPS} =off
RewriteRule ^help/returns https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
This rule will redirect all requests to http://example.com/help/returns to a secure (HTTPS) location: https://example.com/help/returns -- it will preserve full URL path + query string. You have too many conditions, rule becomes complex which is not a good thing when your server is REALLY busy (regular expressions are expensive).
I have replaced %{SERVER_PORT} 80 by more proper %{HTTPS} =off (this especially useful if your site is run on non-default port, which is 80).
I have also removed HTTP_HOST matching part -- you don't really need it unless you have more than one domain name/subdomain bound to the same site. In case if you need this condition just add this line after 1st line: RewriteCond %{HTTP_HOST} ^(mysite.localhost|mylivesite.com)
I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.