redirect everything with exceptions - .htaccess

I am looking for quite some time here to find something similar, but I am unable to find what I am looking for.
I would like to redirect an old Project discarded Project to a new one, they are only partly similar and I would like to redirect the matches, and afterwards everything else, while removing every
I think I remembere that htaccess Rules are worked one after another, so I can simply add 301's above the redirect everything else, but it turns out that this code
RewriteEngine On
Redirect 301 /match1.html https://www.domain.tld/match1/
Redirect 301 /match2.html https://www.domain.tld/match2/
Redirect 301 /match3.php&page=6 https://www.domain.tld/match3/?
RewriteBase /
RewriteRule .*? https://www.domain.tld/? [R=301,L]
redirects simply everything directly to https://www.domain.tld/
The Old Project mas completely manual work with 450+ static html pages and another ~100 php files, so I hope there is a more simple way that creating a huge .htaccess to do this.

Your rule redirecting everything to https://www.domain.tld/ because of your last rule, and for redirecting enbloc you can use regular expression for match try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ https://www.domain.tld/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ https://www.domain.tld/$1/ [R=301,L]
If there are other certain patterns please provide them and I am assuming you are using the rules in root .htaccess or respective directory.

Related

301 redirect a whole directory to a specific thread/link (same domain)

I would like to add a 301 redirect to a whole directory and redirect every single link of this directory to a specific thread/post on the same domain.
I am using an apache server so i suppose i will have to edit the .htaccess
I want all the threads of this directory:
http://example.com/f73
To be redirected there:
http://example.com/f75/newthread-1990/#post333
I have read some different solutions and got confused :(
Some guys recommend to use the "Redirect" command and other recommend the "RedirectMatch" command...
I also read that the command should be different if the .htaccess is already edited and has some rewrite rules. Not sure if it makes sense or if i misunderstood, but at the moment i have added the following lines to the .htaccess:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ htt://example.com/$1 [L,R=301]
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^f73 /f75/newthread-1990/#post333 [L,NC,NE,R=301]

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources