Change port and host using .htaccess [closed] - .htaccess

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am trying to use mod_rewrite to basically port forward a port on a subdomain to another port on another IP.
Like this:
sub.website.com:2000 --> 123.45.67.891:3000
How could this be accomplished using a .htaccess file with mod_rewrite?
I have tried the following but to no avail:
RewriteCond %{HTTP_HOST} ^sub.website.com$ [NC]
RewriteCond %{SERVER_PORT} ^2000$
RewriteRule ^(.*)$ https://123.45.67.891:3000/$1 [L,R=302]

Playing with your rules, I found out that HTTP_HOST includes the port number. So the rules should look like
RewriteCond %{HTTP_HOST} ^sub.website.com:2000$ [NC]
RewriteCond %{SERVER_PORT} ^2000$
RewriteRule ^(.*)$ https://123.45.67.891:3000/$1 [L,R=302]
If you want to test against server name alone, you could use %{SERVER_NAME} as #faa suggested in the comments
RewriteCond %{SERVER_NAME} ^sub.website.com$ [NC]

Related

Remove part from the Url using htaccess [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I ve looked into many examples in stackoverflow and couldn't find a working solution for me.
I have a wordpress blog set up in a subdirectory.
The blog sits under www.domain.co.uk/wordpress/
In order for my permalinks to work I did an htaccess rewrite rule which is this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.co.uk$
RewriteRule ^(/)?$ http://domain.co.uk/wordpress [L]
Is there a way to take out the /wordpress part on the URL?
I need it to point there but I want the /wordpress to be removed.
Any ideas?
I have now managed to solve this issue following this guide: http://www.optiniche.com/blog/145/wordpress-tutorial-install-wordpress-in-a-different-directory/
Remove the wordpress from the redirect in your rule (the http://domain.co.uk/ means there's an implicit redirect) and add a specific rule to internally rewrite instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$ [NC]
RewriteRule ^ http://domain.co.uk/ [L,R=301]
# now silently rewrite to wordpress
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1 [L]

Redirect all requests under a domain to an under construction folder [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have been trying to redirect all requests under a domain to an underconstruction folder with the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/underconstruction/
RewriteRule ^ /underconstruction/ [R=301]
But it doesn't seem to work.
I tried this (and it works):
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/underconstruction/underconstruction.html
RewriteRule ^ /underconstruction/underconstruction.html [R=301]
But I don't see the images and CSS that it comes with it.
Does anyone have any idea?
Maybe one solution is to exclude all image and CSS files from the rule, like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Add or remove file types in the next line if necessary.
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif|bmp|js) [NC]
RewriteCond %{REQUEST_URI} !underconstruction\.html [NC]
RewriteRule .* /underconstruction/underconstruction.html [R=302,L]
Other options are to replace relative with absolute paths in the links to those files or to use the BASE element as described in this answer

Virtual sub domains plus additional parameters [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have the following rules which works fine. It simply give me the ability to make virtual sub domains:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /projects/index.php?project=%2 [L,QSA]
When it's project.domain.com it's totally okay, however, I need to cover up project.domain.com/first/ to domain.com/index.php?project=%2&a=first and project.domain.com/first/second/ to domain.com/index.php?project=%2&a=first&b=second.
I've already tried couple of things, but none of them worked. I can list them but that's gonna be a little bit long with all the description. If anyone could help, that's great, otherwise please comment to update my question with whatever I've already tried and their results.
You may try this:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?([^/]+)?/? /projects/index.php?project=%2&a=$1&b=$2 [L,QSA,NC]
Option
If the redirection is to:
http://domain.com/index.php?project=%2&a=first&b=second
as in the last example, replace the previous rewrite rule with this one:
RewriteRule ^([^/]+)/?([^/]+)?/? http://domain.com/index.php?project=%2&a=$1&b=$2 [L,QSA,NC]

Remove "www." from url with forceful https [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am using the following in my .htaccess file to force https on any user that is visiting my website without https in the url.
However, if the user visits https://www.mysite.co, I want to be able to remove the www from the request.
Please can you tell me what I need to modify in my .htaccess file in order to remove the www from the url while maintaining the forceful https?
Thanks,
Max.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mysite.co/$1 [R,L]
Do it with 2 rewrites.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://mysite.co/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://mysite.co/$1 [R,L]

.htaccess RewriteRule [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I want
http://mysite.com/?p=47&preview=true
to be rewritten to
http://mysite.hostingcompany.com/?p=47&preview=true
But all other requests to http://mysite.com/ to act normally
My .htaccess looks like this:
RewriteCond ^mysite.com\/?p=(.*)\&preview=true$
RewriteRule ^mysite.hostingcompany.com/?p=$1&preview=true$ [L]
But it's not matching. I must be butchering my regular expression but I'm not sure how.
RewriteCond matches against a variable like %{HTTP_HOST} or %{QUERY_STRING}. In your case, you can make use of both of them:
# If the requested host is mysite.com
RewriteCond %{HTTP_HOST} ^mysite\.com$
# And the query string matches
RewriteCond %{QUERY_STRING} p=(\d+)
RewriteCond %{QUERY_STRING} preview=true
# Redirect it, appending the querystring as is
RewriteRule (.*) http://mysite.hostingcompany.com/$1 [L,R,QSA]

Resources