Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [nc]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . url_handle.php
What I am trying to do is have a www rewrite rule so that urls without www. redirect to the same url with www. prefix. I am also using clean URL's, so any unknown URL is sent to url_handle.php for processing.
The problem is that if I have a url like "http://mysite.com/part_1/part_2/part_3", the URL is redirected and then reads "http://www.mysite.com/url_handle.php/part_2/part_3", ommitting the "part_1" and replacing it with "url_handle.php".
Firstly, to redirect non-www sites to www use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.mysite.com/$1 [L,R,NE]
It means, if the url is not "www.mysite.com" and if not blank redirect to "www.mysite.com"
Secondly, to redirect unknown sites to a specific page use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mysite.com/url_handle.php [R=301,L]
The last row will save you :)
Related
I have domain with url https://reg.bmi.id. I'd like to make any user that type anything after the url is redirected to https://reg.bmi.id.
Example:
https://reg.bmi.id/admin will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asidjadhqowidhqohuqw will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asdjqoq/qdoqwun/qowidopq will be redirected in to https://reg.bmi.id
https://reg.bmi.id/contact/contact.php will be redirected in to https://reg.bmi.id
In exception, if the user precisely type https://reg.bmi.id/reg_pilot then it will not be redirected. It open the page of /reg_pilot
This is my current .htaccess which is located in root folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^reg\.bmi\.id$ [OR]
RewriteCond %{HTTP_HOST} ^www\.reg\.bmi\.id$
RewriteRule ^/?$ "https\:\/\/reg\.bmi\.id\/" [R=301,L]
reg/pilot is a reference to any other rsources on this domain. /contact/contact.php is not exist. Anything typed except /reg_pilot after the domain is should be redirected
I have little to no experience dealing about .htaccess any help is greatly appreciated
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Setting rewrite base here.
RewriteBase /
##Checking for non https requests and applying https to it with/without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id/ [R=301,L]
##Stopping/forbidding direct access of index.php here.
RewriteRule ^index\.php$ - [L,NC]
##Any non existing directory/file is served by php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !reg_pilot [NC]
RewriteRule ^ https://reg.bmi.id/ [R=301]
RewriteRule ^ index.php [NC,L]
Currently we have this rule in .htaccess
RewriteEngine on <br>
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
What this does is for any visit we will redirect to subfolder call site if the page not found. And when this happen the url will not show site as subfolder.
Example if we have
/rootFolder/site/temp.html this will show in url as
http://www.domain.com/temp.html
This is working fine but now we need to add https redirect if user visit site.
This is the new rule I came up with
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://%{HTTP_HOST}/site/$1
RewriteRule ^(/)?$ https://%{HTTP_HOST}/site/index [L]
But the issue with this one is now url will show site subfolder
https://www.domain.com/site/temp.html
How can I achieve so that if user does
http://www.domain.com/temp.html it will find temp.html in site subfolder and redirect to https and url will only show
https://www.domain.com/temp.html
Thanks
How about if you do it in two passes, always redirecting whatever was requested to https, then running your rewrite rules, like so:
RewriteCond %{HTTPS} off
#301 flag redirects instead of rewriting
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#leaving your original rules as is
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
I have a htaccess redirect (without url change) to a folder where my site is. Recently i got a new domain name, but i can't get the htaccess code working on that domain.
main domain: www.test.com
folder of site: /drupal
this is the htaccess code i use (so users go to www.test.com and see the /drupal conten)
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
RedirectMatch 301 ^/drupal/$ http://www.test.com/
Now, i have a new domain www.secondtest.com on that server, which content stands on /drupal2 but whenever i type www.secondtest.com into my browser he redirects me to www.test.com .
Is there a way to modify the htaccess like so?
www.test.com (go's invisible to folder) --> www.test.com/drupal
www.secondtest.com (go's invisible to folder) --> www.test.com/drupal2
You have to use an another RewriteCond to check the domain :
# Redirection if not www.test.com nor www.secondtest.com
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.secondtest\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
# redirection to drupal folder if test.com
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
# redirection to drupal2 folder if secondtest.com
RewriteCond %{HTTP_HOST} ^www\.secondtest\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal2/index.php?q=$0 [QSA]
I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]
I want to redirect visitors to my main domain when they perform requests on my subdomain followed by a not matching URI.
For example, a visitor can access a resource at sub.domain.com/product/10 but he should be redirected to domain.com when he attempt to access other resources that not match product/:id on my subdomain like sub.domain.com/anOtherResource.
I have to do this with apache rewrite module. I found that ! operator can do the job but it's not working for me.
Here is my Rewrite configuration from .htaccess file :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
I also tested this configuration :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule !^/product/[0-9]+$ http://www.domain.com [L,R]
I don't know where is the mistake ...
[Edit]
The .htaccess file is configured for Wordpress. Here is the entire .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The problem was in the Wordpress Rewrite configuration so what's happened ?
Step 1
I perform a request at sub.domain.com/product/1 so it not match these RewriteCond :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
Then, it not redirect to www.domain.com
Step 2
It continue the rewriting to the next Cond (Worpdress rewriting):
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
At this step, it send an Internal Redirect (not sent to the client) to sub.domain.com/index.php
Step 3
Because of the redirection, it apply again the previous RewriteCond
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
It match the Cond, then it redirect the client to www.domain.com while I didn't expect it.
The Solution
I fixed the problem by adding a RewriteCond on index.php as showing bellow :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
Now it manage the Internal Redirection sent by the Worpress Rewriting.
The rewrite rules look ok, but try the following:
RewriteCond %{HTTP_HOST} ^sub[.]domain[.]com$
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
If this doesn't work, then you might need to copy and paste the entire contents of your VirtualHost configuration since it could be something else causing a problem.