301 Redirect Conflict with www redirect - .htaccess

I am having an issue with my 301 redirects.
I just upgraded my site from an html based site to a joomla site. SO I am trying to redirect the 50 or so pages to the new joomla based navigation.
SO what is working:
rewrite rule to remove index.php, and the www., and 301 redirects
What isn't working:
301 redirect with a www in front of it.
www.sample.com/page.html
It sends them to the home page instead of the page, it takes them to the home page.
Here is my www rewrite rule.
## Redirects to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} www.sample.com
RewriteRule (.*) http://sample.com/$1 [R=301,L]
Here is my 301 rule
Redirect 301 /page.html /page
Thanks for the help.

If you are trying to remove the www then you might want to change the matching up some. Try your rule like this.
## Redirects to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.sample\.com$ [NC]
RewriteRule (.*) http://sample.com/$1 [R=301,L]
RewriteRule ^page.html$ /page [R=301,L]
RewriteRule ^page2.html$ /page2 [R=301,L]

Related

How to stop RewriteRule overruling Redirect 301 in htaccess

I would like to make some massive redirects in .htaccess because of a migration. I'm using both RewriteRule and Redirect 301. But the RewriteRule keeps overwriting the Redirect 301 rules, despite of the order.
For example:
I would like to redirect /retraites-nl-cat-595.html to https://www.vihara.nl/meditatieretraites/
But all the other url's that contains 595 to https://www.vihara.nl/tag/vipassana/
I have this in .htaccess:
RewriteEngine on
RewriteRule ^$ https://www.vihara.nl [R=301]
Redirect 301 /retraites-nl-cat-595.html https://www.vihara.nl/meditatieretraites/
RewriteRule ^(.*)595(.*)$ https://www.vihara.nl/tag/vipassana/ [L,R=301]
But this is not working because the RewriteRule is overruling the Redirect 301 rule. The same happens when I switch the order of the last 2 lines.
Is there a way to change this? So the Redirect 301 will be overruling the Rewrite rule, instead of the other way around?

Redirect 301 with condtion

I can redirect all traffic to new blog subdirectory like:
Redirect 301 / https://new-website.com/blog/
However, I want this redirect to happen ONLY if it comes from old website subdomain referral:
https://blog.old-website.com
Any idea?
You can use mod-rewrite
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https://blog\.oldsite\.com
RewriteRule ^.*$ http://newsite.com/blog%{REQUEST_URI} [L,R]

Redirect 301 specific pages and the rest to just one page

First I need to redirect these pages to another page in a different domain
Redirect 301 /example1 http://newdomain.com/test1
Redirect 301 /example2 http://newdomain.com/random1
Note the pages are not the same in the new domain (e.g., /example1 to /test1)
After that, I need redirect the rest of the pages to newdomain.com
E.g., Redirect 301 (everything else) to http://newdomain.com
Try below rule, using mod rewrite I am assuming you have mod rewrite enabled.
RewriteEngine On
RewriteRule ^example1$ http://newdomain.com/test1 [R=301,L]
RewriteRule ^example2$ http://newdomain.com/random1 [R=301,L]
RewriteCond %{REQUEST_URI} !^(example1|example2)
RewriteRule ^ http://newdomain.com [R=301,L]
If you want to use mod-alias , you can use these redirects :
RedirectMatch 301 ^/example1/?$ http://example.com/test1
RedirectMatch 301 ^/example2/?$ http://example.com/random1
#redirect everything else to the homepage of example.com
RedirectMatch ^.+$ http://example.com/
Clear your browser cache before testing these redirects.
Try this in your .htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

Old Domain 301 Redirect to New Domain with HTAccess Wildcard

I have a old domain lets call it
example.com
I want to do a 301 redirect of all its page and its homepage to
newsite.com
So for example
example.com/category/page.html
Should 301 redirect to
newsite.com/category/page.html
And also
example.com should redirect to newsite.com
Be it with www or without www
I tried the following
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.newsite.tv/$1 [L,R=301,NC]
My folder structure is
index.html
.htaccess
When I go in the site, be it with wildcard or not, it only load the index.html and the .htaccess redirect is not working.
I did enable modrewrite
Can anyone guide me to set up the right .htaccess for this case.
Thanks!
You can also use RedirectMatch directive .
Try this in Olddomain/.htaccess :
RedirectMatch ^/(.*)$ http://newsite.com/$1

Redirects in htaccess file not working

I have an htaccess file with over 700 redirects and most of them are not working at all. I get a 404 page mostly even though they are on the same domain. Here's 5 of them that aren't working:
Redirect 301 /orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-1-person.html /fly-snooze-cruise/orlando-airport/
Redirect 301 /orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-2-people.html /fly-snooze-cruise/orlando-airport/
Redirect 301 /orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-3-people.html /fly-snooze-cruise/orlando-airport/
Redirect 301 /orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-4-people.html /fly-snooze-cruise/orlando-airport/
Redirect 301 /orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-5-people.html /fly-snooze-cruise/orlando-airport/
I have checked the domains Please tell me what I am doing wrong. All help is appreciated.
You can use only one:
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-\d+-person\.html$ /fly-snooze-cruise/orlando-airport [R=301,L]
OK, I found out that the rewrite rules in the mod_rewrite section somehow messed with the redirects. So I converted the "Redirect 301"s to Rewrite Rules in mod_rewrite.c and everything is working.
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-1-person.html$ /fly-snooze-cruise/orlando-airport [R=301,L]
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-2-people.html$ /fly-snooze-cruise/orlando-airport [R=301,L]
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-3-people.html$ /fly-snooze-cruise/orlando-airport [R=301,L]
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-4-people.html$ /fly-snooze-cruise/orlando-airport [R=301,L]
RewriteRule ^orlando-airport/fly-snooze-cruise-hotel-packages-orlando-airport-5-people.html$ /fly-snooze-cruise/orlando-airport [R=301,L]

Resources