How do I rewrite all requests from a specific domain name to my site with .htaccess?
For example, if http://www.example.com had a link to my site, then I want to redirect them to a different page on my site, but other links from other websites and direct links should display the requested page like normal.
I've been getting in a muddle with how to do this... Thanks in advance!
UPDATE:
Some information as requested in an answer:
My stripped down .htaccess file looks like this:
# irrelevant all squashed together (issue still remains when used like this)
<Files .htaccess>
order allow,deny
deny from all
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteOptions MaxRedirects=10
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301]
ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
ErrorDocument 500 /error.html
Options All -Indexes
IndexIgnore *
LimitRequestBody 10240000
#redirect
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.com/ [NC]
RewriteRule !^error\.html /error.html [L,NC,R=302]
The header of the page I am visiting looks like this:
Referer: http://www.badsite.com/wiki/Page
2nd UPDATE:
The intended actions are:
Visit http://www.example.com/wiki/Page
Click on link to http://www.mysite.co.uk/thispage/here
Because of the example.com, I should be redirected to http://www.mysite.co.uk/badpage instead of http://www.mysite.co.uk/thispage/here like all other requests.
You will need to rely on HTTP_REFERER for this. Put this code in your sub-directory's .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^id=[^&]+ [NC]
# if referrer is bad.com
RewriteCond %{HTTP_REFERER} (www\.)?bad\.com [NC]
# then redirect to a different page
RewriteRule !^start start [L,NC,R=302]
Though remember that HTTP_REFERER can be spoofed and is not 100% reliable.
PS: It turned out to be unique situtation due to dokuwiki installation
Related
I have this in my htaccess
Options +FollowSymlinks
RewriteEngine On
Deny from all
#RewriteRule (.*) http://google.com [R=301,L]
Allow from 127.0.0.1
Allow from 89.32.93.99
RewriteRule index.php$ /otherfolder [R=301,L]
I am trying to redirect all the requests to a new folder but for some reason the above does not work.
I have also tried
Redirect 301 / /otherfolder
but that just added other folder again and again to my domain like this
mydomain.com/otherfolder/otherfolder/.....
thanks
Keep your rules like this:
Deny from all
Allow from 127.0.0.1
Allow from 89.32.93.99
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(index\.php)?$ /otherfolder [NC,R=301,L]
^(index\.php)?$ will match either / or /index.php in URL.
I have a task which drives me nuts: I have an old blog and want to create a 301 redirect to my new blog for several posts because I moved some of the content to the new domain.
Let's say one of my old posts is to be find at example.com/this-is-my-post and visitors should be redirected to example.org/my-old-post-2817.
What I tried is this:
# BEGIN Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post$ http://example.org/my-old-post-2817 [R=301]
</IfModule>
# END Redirect
The problem is that there is no redirect in FF (desktop), Chrome (Android) and standard internet browser (Android). In IE (desktop) I get an error stating that the page can't be displayed. No reason is given.
I made sure not to have any unneccessary blank lines in the .htaccess and, to be on the safe side, deleted my browser's histories, caches and so on.
A CCA of my provider dropped me a message that he re-saved the mod rewrite module and that I have to try it 15 minutes later again. With no success.
So, what did I wrong?
Regards,
Crunchy
Change order of rules to keep R=301 rules before internal WP rules:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post/?$ http://example.org/my-old-post-2817 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
I'm trying to do a redirect but for some reason I am getting an apache error page using the code below
# -> Rewrite START
Options ExecCGI +FollowSymLinks
RewriteEngine On
ErrorDocument 403 http://example.com
### Deny website access to users outsdie our IP addresses
order deny,allow
deny from all
allow from xx.xx.xx.xx
allow from xx.xx.xx.xx
### Deny end
EDITED
Could someone please point me in the right direction. I'm new to .htaccess. How would i add another IP address to the code below?
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=xx.xx.xx.xx
RewriteRule ^(.*)$ http://example.com [L,R=301]
You're getting errors because of looping since request to your 403 will also cause 403 (since IPs will still match).
Try this code instead:
Options ExecCGI +FollowSymLinks
ErrorDocument 403 /
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(xx\.yy\.zz\.mm|aa\.bb\.cc\.dd)$
RewriteRule ^.+$ - [F]
I have tried url redirection of HTTP to HTTPS on my website by using several different solutions in .htaccess and none have worked.
Well, actually it does work if I type in directly:
(only for example) http://mywebsite.com
into the browser address bar....but if I try to redirect the url any other way, it won't work. If I try the link to my page listed on Google http://mywebsite.com/ it will not redirect to https://www.mywebsite.com. I need it to redirect from HTTP to HTTPS if someone clicks the link to my page from a search engine.
So far I have tried:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
then I tried...
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and then I tried...
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
None worked. What else should I try? (this website is being hosted on a web hosting service site that has uses an apache web server). Also, I have an SSL certificate for the page, if that makes any difference.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Tip: Clear your browser cache or use a different browser to test.
I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html