I need to prevent direct access to a URL (http://www.example.com/gated-asset). Is there any way to add code to the htaccess file that would redirect all direct traffic to another page (http://www.example.com/form)?
I have tried the following code in my htaccess file, but all pages, including the home page, redirect to www.example.com/form.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
The entire .htaccess file looks like this (it is a WordPress site):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
I have also tried the following:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule ^/$ /form/ [R,L]
RewriteRule ^/$ /gated-asset/ [R,L]
As well as:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule http://www.example.com/gated-asset http://www.example.com/form/ [R,L]
You need to include http:// in the referrer check if you are matching the beginning (^). Unlike HTTP_HOST, HTTP_REFERER requires it. In addition, the referring URL may include a REQUEST_URI, and so closing the expression at the domain will prevent it from working.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule .* http://www.example.com/form [R,L]
If you don't want to include the http://, then you can use the following condition instead:
RewriteCond %{HTTP_REFERER} !go.example.com [NC]
However, I would recommend the first example be used.
Also included here are the R and L flags. R is implied if the rewrite is to an external resource, but it is generally better to include it.
Related
I have this example URL
www.something.com/order?_vacation=bahamas
The Value "Bahamas" can vary of course based on the location
I would like to redirect this URL
www.something.com/order
If anyone was typing it in the address bar without the parameter _vacation and it's value
Let's say - Redirect him to the homepage
www.something.com
Can anyone suggest how to do it using htaccess?
Thanks
Update here is my htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)_vacation=[^&]+ [NC]
RewriteRule ^order/?$ /? [L,NC,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tripwith/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tripwith/index.php [L]
</IfModule>
# END WordPress
You may use this rule as your topmost rule for this purpose:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)_vacation=[^&]+ [NC]
RewriteRule ^order/?$ /? [L,NC,R=301]
I am helping someone with their site and they want a template page (example.com/template) shown on each of their subdomains.
So when a user navigates to a.example.com or b.example.com, etc.they see example.com/template.php but the url stays a.example.comor b.example.com, etc.
I know there is a way to do this I just can't figure it out.
Basically I want to be able to change all of the pages at once by making a change to 1 file.
I think I am close with this code but I keep get 500 errors
RewriteCond %{HTTP_HOST} ^$.example.com$ [NC]
RewriteRule ^$ $1.example.com/template.php
PS: these are actual domains, not virtual
This is what I currently have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.+\.dfwfamilylifenews\.com [NC]
RewriteRule ^$ /template.php [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Firstly, make sure the rewrite module is loaded in Apache. See this answer for more info.
Second, you need to change your rules to:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^.+\.example\.com [NC]
RewriteRule ^$ /template.php [L]
Trying to set 301 redirect in .htaccess file and here is what i am trying to do
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*)$ http://www.mysite.com/wordpress/$1 [R=301,L]
I am testing this on my local machine using WAMPP server.Though when i hit http://localhost/wordpress/ i am getting redirected to http://www.mysite.com/wordpress/ but for other URL i am not getting redirected at all.for e.g
I have this URL in my local machine http://localhost/wordpress/2010/11/shadows/ and this at the server http://www.mysite.com/wordpress/2010/11/shadows/ but when i hit this URL i am not getting redirected to respected URL on the live server ,but i am being showed same page from local machine.
Working:
http://localhost/wordpress/
=> Redirected to:
http://www.mysite.com/wordpress/
Not working
http://localhost/wordpress/2010/11/shadows/
=> Redirected to:
http://www.mysite.com/wordpress/2010/11/shadows/
As clear from URL, I am trying to do this in Wordpress.
Here is complete .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*)$ http://www.mysite.com/wordpress/$1 [R=301,L]
Can any one tell me whats wrong with the redirection entry? Thanks in advance
Update
I have even tried this option
Options +FollowSymLinks
RewriteEngine on
RewriteBase /wordpress/
RewriteRule ^(.*)$ http://www.mysite.com/wordpress/$1 [L,R=301]
Did not worked.
Always you first then the others (talking about RewriteRules only, man! :D ) (and you've forgotten the QSA directive).
So here's the "clean" version of your RewriteRule:
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN My Own rewrite rules
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*) http://www.mysite.com/wordpress/$1 [QSA,R=301,L]
# END My Own rewrite rules
# BEGIN WordPress
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
# END WordPress
</IfModule>
If it's in a .htaccess file then try without the / like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN My Own rewrite rules
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
# Without the / after wordpress:
RewriteRule (.*) http://www.mysite.com/wordpress$1 [QSA,R=301,L]
# END My Own rewrite rules
# BEGIN WordPress
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
# END WordPress
</IfModule>
By the way this is the first time in many years that a find an "under construction" page nice!
Please tell me if it works.
%{HTTP_HOST] will contain something like localhost or www.thecolorsofmysoul.com. So your conditions will never match.
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
and the redirecting to the external domain will never fire.
Also the first two rule act in consort to map any non-file/directory to index.php. http://localhost/wordpress/ has a regexp match string of "" so will fail the pattern "." and will fail through and will redirect with your "update". Try
Options +FollowSymLinks
RewriteEngine on
RewriteBase /wordpress/
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^.* http://www.thecolorsofmysoul.com/wordpress/$0 [L,R=301]
BTW with this base this should be in DOCROOT/.htaccess. The corresponding Wordpress rules (which shouldn't be above this redirect) are
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$) index.php [L]
You don't need to repeat the base in the target and the negative lookahead assertion removes the need for the first index.php rule.
I have a wordpress site. Here is link to site. I want to redirect www.abc.com/?portfolios=letters to www.abc.com/?files=letters.
I tried editing in code, but din't worked.
Here is htaccess
Options +FollowSymLinks
RewriteEngine on
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^womackpi.com/?portfolio=(.+) womackpi.com/?files=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This should do:
RewriteRule ^abc.com/?portfolios=(.+) abc.com/?files=$1 [NC]
It works for me!
You will also need:
Options +FollowSymLinks
RewriteEngine on
Above the previous statement.
Query strings are not part of the URI that is used to match against the expression in RewriteRule..
Try replacing RewriteRule ^womackpi.com/?portfolio=(.+) womackpi.com/?files=$1 [NC] with:
RewriteCond %{HTTP_HOST} ^(www\.)?womackpi.com$ [NC]
RewriteCond %{QUERY_STRING} (^|&)portfolio=(.+)(&|$) [NC]
RewriteRule ^(.*)$ /$1?files=%2 [L]
Add an R inside the [L] brackets if you want to redirect the browser (changing the URL in the address bar).
Trying to set up a 301 redirect for a change in domain. I'm using Textpattern which already has a mod rewrite. When adding the redirect I'm prompted with a error page on the site, stating that the page has resulted in too many redirects!
This is the htaccess file in use...
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /relative/web/path/
RewriteRule (.*) http://www.domain.com/$1 [R=301,L] #(this has been added to the default textpattern htaccess file)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
Many thanks for any advise you can give.
From what domain you try to redirect? May be you have set cycling - trying to redirect from site.com to site.com?
Add this after RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www.yournewdomain.com$).*$
RewriteRule (.*) http://www.yournewdomain.com/$1 [R=301,L]