.htaccess RewriteRule [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 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]

Related

Generic rules to rewrite urls [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to rewrite my urls for my website.
I've 5 pages and for all of them, I need to insert a new RewriteRule like this one:
RewriteRule about$ about.php [L,QSA]
RewriteRule contact$ contact.php [L,QSA]
RewriteRule ^blog/([^/]+)$ article.php?i=$1 [L,QSA]
Is there a way I can make it more generic ?
Thanks.
You probably are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,END]
RewriteRule ^/?blog/([^/]+)$ /article.php?i=$1 [QSA,END]

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

How do I make sure my site always has the https infront [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
Hey guys I have the following .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.site.com$ [NC]
RewriteRule ^(.*)$ https://www.site.com/$1 [L,R=301]
This will always make sure my ssl is properly showing, but for some reason on google if you are to search the site and than click on its subcatagorys, like services. It just takes you to site's services and displays it without the https, so it just becomes www.....
How do I fix this?
David
the rewritecond you're using is preventing the urls with www (eg http:// www.site.com/page.html) from being redirected
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.site.com%{REQUEST_URI} [R=301,L]

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]

Change port and host 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 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]

Resources