301 redirects how to phrase 'anything else' - .htaccess

How do I phrase a 301 redirect that is effectively 'anything else'? In other words I have a specific list of pages to redirect but I simply want anything that is not specifically redirected to go to the home page

The line: RewriteCond %{REQUEST_URI} !/$ stops the infinite redirect loop from root to root.
RedirectMatch 301 ^pag1.html$ /new_pag1.html
RedirectMatch 301 ^pag2.html$ /new_pag2.html
RewriteEngine On
RewriteCond %{REQUEST_URI} !/pag1.html$
RewriteCond %{REQUEST_URI} !/new_pag1.html$
RewriteCond %{REQUEST_URI} !/pag2.html$
RewriteCond %{REQUEST_URI} !/new_pag2.html$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ / [R=301,L]

Related

Don't want Redirect 301 to affect subfolder in /public_html

I have this rule in .htaccess:
Redirect 301 /index.php https://newdomain.com/
But I still want to use a folder at olddomain.com ... e.g., I want to visit olddomain.com/subfolder/ but instead I'm getting redirected to newdomain.com/subfolder/, which doesn't exist of course.
I've also tried this:
RewriteEngine on
RewriteRule !^subfolder($|/) https://newdomain.com%{REQUEST_URI} [L,R=301]
(as suggested here: https://www.fdgweb.com/exclude-sub-directory-htaccess-301-redirects/), but it didn't work either.
Is there a way to have a subfolder as an exception to the 301 redirect? Or, instead, is there a way to have the 301 redirect only apply to the folder the .htaccess file is in?
I managed to get the redirect to apply only to /index.php, which means i can still access olddomain/subfolder:
RewriteCond %{HTTP_HOST} ^olddoman\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Sectigo\ DCV)?$
RewriteRule ^index\.php$ "https\:\/\/newdomain\.com\/" [R=302,L]

htaccess redirect not working for inner pages / folders

Ok, so I've got a stack of htaccess redirects from one domain to another, which for the most part works fine. However, this particular link isn't working:
http://giccampers.com.au/general-trailers/tipper-trailer/
It should take people to this website:
http://blackseriescampertrailers.com.au/
If you go to the main domain of http://giccampers.com.au/ it redirects fine. But I can't seem to get any of the other pages to redirect from that domain.
Here's what I've got (I didn't set these up, btw):
RewriteCond %{HTTP_HOST} ^giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^giccampers\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.giccampers\.com\.au$
RewriteRule ^/?$ "http\:\/\/www\.blackseriescampertrailers\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
redirect 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
Any help would be great!
use this
RedirectMatch 301 http://giccampers.com.au/general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
RedirectMatch 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /tipper-trailer$ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /general-trailers/tipper-trailer(.*)http://blackseriescampertrailers.com.au/

Htaccess redirects from digits at the end

I want to make redirects from urls like
/test/112321 to /test/
/test/test2/1311223 /test/test2/
There are only digits in the end of the url.
Now i have
RewriteCond %{HTTP_HOST} ^(.*)([0-9]*)/$ [NC]
RewriteRule ^(.*)/([0-9]*)$ http://%1/$1/ [R=301,L]
but it doesn't work.
Could you help me with this?
You don't need RewriteCond %{HTTP_HOST} as you're matching REQUEST_URI which you can do in RewriteRule itself.
You can use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^(.+)/([0-9]+)/?$ /$1/ [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(.*)/([0-9]+)/?$
rather than:
RewriteCond %{HTTP_HOST} ^(.*)([0-9]*)/$
You can also use RedirectMatch
RedirectMatch 301 ^/(.+)/([0-9]+)/?$ /$1

How do I redirect subdomains that do not exist?

I am trying to redirect using .htaccess in the following fashion. I am not all that familiar with .htaccess, so I'm not sure it can be done. Also, I don't know if how I am intending to do it follows best practices for SEO.
www.domain.com > domain.com 301
ks.domain.com > kansas.domain.com 301
ia.domain.com > iowa.domain.com 301
domain.com/sites > domain.com 301
domain.com/sites/iowa > iowa.domain.com 301
nonexistent.domain.com > domain.com 302
domain.com/sites/nonexistent > domain.com 302
My biggest question is if I can detect a nonexistent subdomain and redirect. I would love to see how all of the above is accomplished.
First, you need to add wildcard subdomains by creating a subdomain with an * as its name, only if your web host allows you to do so. And this must be in your .htaccess, try to test it to see if it works:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^ks\.domain\.com
RewriteRule ^(.*)$ http://kansas.domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^ia\.domain\.com
RewriteRule ^(.*)$ http://iowa.domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/?$
RewriteRule ^(.*) / [R=301]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/iowa/?$
RewriteRule ^(.*) http://iowa.domain.com/ [R=301]
RewriteCond %{HTTP_HOST} ([a-z0-9-]+)\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) http://domain.com/ [R=302]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/([a-z0-9-_]+)/?
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*) http://domain.com/ [R=302]
Just use -f to test if a requested file exists and is a regular file, -s if it exists and has a file size greater than 0 and -d to test if it exists and is a directory.
If you want specific subdomains that do not exist, well you'll just have to create them, and then redirect.
To catch all erroneous subdomains, say I accidentally type metaa.stackoverlow.com, use a wildcard: *.stackoverflow.com. In cpanel, this just involves ticking a checkbox that asks 'make wildcard?' or similar. To edit .htaccess directly, just enter * in place of each specific subdomain.
Note that this also applies for any directories:
subdomain.site.com/*
*.site.com/dir
*.site.com/*

htaccess redirect problem

I have the following redirects in my htaccess file
Redirect 301 /communityed/index.htm http://www.domain.com/community_education
Redirect 301 /communityed http://www.domain.com/community_education
Redirect 301 /communityed/visit/index.htm http://www.domain.com/directions/
Redirect 301 /communityed/visit/pdfs/campus_map.pdf http://www.domain.com/images/uploads/pdf/campusmap.pdf
The first two redirects work perfectly, but the last two are not.
When to go domain.com/communityed/visit/index.htm it redirects it to domain.com/community_education/visit/index.htm, but it should go to domain.com/directions
Same thing the 4th redirect as well
http://domain.com/communityed/visit/pdfs/campus_map.pdf redirects to http://www.domain.com/community_education/visit/pdfs/campus_map.pdf, when it should really go to http://www.domain.com/images/uploads/pdf/campusmap.pdf
What am I doing wrong that the last 2 redirects are not working appropriately?
I imagine it has something to do w/ the first two redirects over-riding it, but I am fairly new to redirects.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
Given that your redirects are not standard mod_rewrite looking, I'll assume that the matches are greedy, therefore you should change the odder from finest match to most general.
Redirect 301 /communityed/visit/pdfs/campus_map.pdf http://www.domain.com/images/uploads/pdf/campusmap.pdf
Redirect 301 /communityed/visit/index.htm http://www.domain.com/directions/
Redirect 301 /communityed/index.htm http://www.domain.com/community_education
Redirect 301 /communityed http://www.domain.com/community_education

Resources