How do I redirect with .htacess?

tazarbm

Elite Member
Executive VIP
Jr. VIP
Joined
Oct 28, 2020
Messages
9,482
Reaction score
9,778
Hi everyone!

First of all, mods please move this thread to the right section if it doesn't belong in here. I couldn't find a .htaccess-specific thread, that's why I'm putting it in here.

Now, I just purchased an expired domain and I want to redirect all of its unknown / meaningless URLs to the homepage.

I did find a few URLs that still have backlinks from wikipedia and reddit, so I'm gonna manually redirect those manually and individually to care for them properly, but all other URLs (there's 100s of them) I don't feel like doing manually, and I don't care about them, either. But the reason for which I want to redirect them to the homepage instead of just letting them go is because they might still carry some weight... or still get traffic occasionally and I don't want all this value to go to waste.

Besides, I don't want my website to be full of 404 pages, so redirecting these obscure URLs to a live page on my website is the right thing to do... I think. But the problem is that I don't know how to do it with .htaccess, and I can't find a tutorial for this in DDG, either (well, I didn't search thoroughly to be honest, I just checked the first 3-4 pages and then moved on).

But anyway, can someone who is familiar with .htaccess and redirects share the code I need to use? I'm sure it's not complicated, but it's probably one step above my knowledge of htaccess, so I don't know it :p

Please let me know what the code for this type of redirection is if it's not too much trouble.

Thanks!
 
Try this one out..

Code:
https://stackoverflow.com/a/13264852/1437261

Thanks :)

Does it matter where exactly I write that code in? Currently, the .htaccess of my domain looks like this

#+PHPVersion
#="php73"
AddHandler x-httpd-php73 .php
#-PHPVersion

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.

# END WordPress
#+StackCache
#={"images":"A86400","css":"A86400","javascript":"A86400"}
ExpiresActive ON
ExpiresByType "image/jpeg" "A86400"
ExpiresByType "image/jpg" "A86400"
ExpiresByType "image/gif" "A86400"
ExpiresByType "image/png" "A86400"
ExpiresByType "image/svg+xml" "A86400"
ExpiresByType "image/webp" "A86400"
ExpiresByType "image/vnd.microsoft.icon" "A86400"
ExpiresByType "image/x-icon" "A86400"
ExpiresByType "image/ico" "A86400"
ExpiresByType "font/ttf" "A86400"
ExpiresByType "font/otf" "A86400"
ExpiresByType "application/x-font-opentype" "A86400"
ExpiresByType "application/x-font-woff" "A86400"
ExpiresByType "application/x-font-ttf" "A86400"
ExpiresByType "application/font-woff" "A86400"
ExpiresByType "application/vnd.ms-fontobject" "A86400"
ExpiresByType "text/css" "A86400"
ExpiresByType "text/javascript" "A86400"
ExpiresByType "application/javascript" "A86400"
#-StackCache
 
Thanks :)

Does it matter where exactly I write that code in? Currently, the .htaccess of my domain looks like this
In this case it does not matter where you put it, as you don't have any other rules in there ( i assume you will have once you install wp). I'd rather suggest you using a plugin to do the redirect, so that you don't end up modifying wp's own code for the htaccess.

A plugin like this could work..
Code:
https://wordpress.org/plugins/all-404-redirect-to-homepage/
 
I'll check it in a moment (and yeah, wildcard sounds like what I need), but... doesn't the wildcard redirect all URLs?? I need to check this...
You can all URLs or a directory (folder).
 
In this case it does not matter where you put it, as you don't have any other rules in there ( i assume you will have once you install wp). I'd rather suggest you using a plugin to do the redirect, so that you don't end up modifying wp's own code for the htaccess.

A plugin like this could work..
Code:
https://wordpress.org/plugins/all-404-redirect-to-homepage/
oh, that's good to know... Thanks, mate!

You can all URLs or a directory (folder).
got it :)
 
Try this one out..

Code:
https://stackoverflow.com/a/13264852/1437261
Thanks this part of stuff always kind of confused me and Neon say thanks to you .

I did find this guy Neon turns out he's my third cousin he told me this "Gogal owes me £20,000 worth of Indian food " so I am willing to collect for him if you want ?
 
I'll check it in a moment (and yeah, wildcard sounds like what I need), but... doesn't the wildcard redirect all URLs?? I need to check this...

I wouldn't recommend redirecting all URL's to 200 status code, if you do so all your actual 404's will not be seen as 404. This will probably fk up your site in Google's eyes.

I recommend setting up a redirect plan and implementing these in your htaccess.

So basically rewrite as many specific URL's to keep the "value" of the URL and if you have to do mass of URL's under a slug you can do it "category/slug" level

Example:

Old blog URL is /my-blog-library/this-is-my-blog/ (slug/category level), the following code will rewrite any URL that begins with the slug /my-blog-library/ to /new-blog-library/

Code:
rewrite ^/my-blog-library/ /new-blog-library/ permanent;

For specific URL's you have to keep in mind that ur using regex here so you need to close the url with a $, like this:

Code:
rewrite ^/specific/url(/)?$ /new-specific-url/ permanent;

Keep in mind that (/)? stands for rewrite with and without trailing slash and the $ is like closing the regex statement

But DON'T rewrite all your 404's to 200
 
Last edited:
Here is more info from the official Apache website, good luck!:

Code:
https://httpd.apache.org/docs/2.4/rewrite/intro.html
 
I wouldn't recommend redirecting all URL's to 200 status code, if you do so all your actual 404's will not be seen as 404. This will probably fk up your site in Google's eyes.

I recommend setting up a redirect plan and implementing these in your htaccess.

So basically rewrite as many specific URL's to keep the "value" of the URL and if you have to do mass of URL's under a slug you can do it "category/slug" level

Example:

Old blog URL is /my-blog-library/this-is-my-blog/ (slug/category level), the following code will rewrite any URL that begins with the slug /my-blog-library/ to /new-blog-library/

Code:
rewrite ^/my-blog-library/ /new-blog-library/ permanent;

For specific URL's you have to keep in mind that ur using regex here so you need to close the url with a $, like this:

Code:
rewrite ^/specific/url(/)?$ /new-specific-url/ permanent;

Keep in mind that (/)? stands for rewrite with and without trailing slash and the $ is like closing the regex statement

But DON'T rewrite all your 404's to 200

Here is more info from the official Apache website, good luck!:

Code:
https://httpd.apache.org/docs/2.4/rewrite/intro.html

f*ck! Whenever I hear the word "regex" I get shivers, that thing is like the boogeyman for a non-techy guy like me...

Still, I appreciate the warning. Gonna read the crap out of all the links you guys have sent me, and hopefully, in the next 6-7 hours I will become an expert at.... understanding basic htaccess / redirection rules :)
 
Last edited:
f*ck! Whenever I hear the word "regex" I get shivers, that thing is like the boogeyman for a non-techy guy like me...

Still, I appreciate the warning. Gonna read the crap out of all the links you guys have sent me, and hopefully, in the next 6-7 I will become an expert at.... understanding basic htaccess / redirection rules :)
Hahaha, good luck mate

Tip for learning regex, just try some statements on a regex debug site like this:

Code:
https://regex101.com/

When you understand why it does what it does ,making redirects in htaccess will be easy

Just add rewrite in front of your regex statement and the final url and if the redirects is permanent or temporary.

Code:
RewriteRule <REGEX> <NEW URL> [R=301,L] or 302 for temporary
 
Last edited:
Hahaha, good luck mate

Tip for learning regex, just try some statements on a regex debug site like this:

Code:
https://regex101.com/

When you understand why it does what it does ,making redirects in htaccess will be easy

Just add rewrite in front of your regex statement and the final url and if the redirects is permanent or temporary.

Code:
rewrite <REGEX> <NEW URL> [R=301,L] or 302 for temporary
in your last quote you did put "rewrite" in front of the regex statement like you said, but where's the "rewrite" in front of the final URL?? I'm not seeing it. And what's the final URL exactly??

And yeah, understanding regex is a superpower, I know. But it's one that I dread acquiring cause my poor brain can't handle all of those alien symbols, all gathered in one place :D
 
in your last quote you did put "rewrite" in front of the regex statement like you said, but where's the "rewrite" in front of the final URL?? I'm not seeing it. And what's the final URL exactly??

And yeah, understanding regex is a superpower, I know. But it's one that I dread acquiring cause my poor brain can't handle all of those alien symbols, all gathered in one place :D
I just realized I entered a client custom environment htaccess variables instead of standard, i will edit it in a sec :)
 
I just realized I entered a client custom environment htaccess variables instead of standard, i will edit it in a sec :)
did you edit it? I'm not noticing the difference
 
I wouldn't recommend redirecting all URL's to 200 status code, if you do so all your actual 404's will not be seen as 404. This will probably fk up your site in Google's eyes.

I recommend setting up a redirect plan and implementing these in your htaccess.

So basically rewrite as many specific URL's to keep the "value" of the URL and if you have to do mass of URL's under a slug you can do it "category/slug" level

Example:

Old blog URL is /my-blog-library/this-is-my-blog/ (slug/category level), the following code will rewrite any URL that begins with the slug /my-blog-library/ to /new-blog-library/

Code:
rewrite ^/my-blog-library/ /new-blog-library/ permanent;

For specific URL's you have to keep in mind that ur using regex here so you need to close the url with a $, like this:

Code:
rewrite ^/specific/url(/)?$ /new-specific-url/ permanent;

Keep in mind that (/)? stands for rewrite with and without trailing slash and the $ is like closing the regex statement

But DON'T rewrite all your 404's to 200

Well i can't edit it anymore so here you go:

Old blog URL is /my-blog-library/this-is-my-blog/ (slug/category level), the following code will rewrite any URL that begins with the slug /my-blog-library/ to /new-blog-library/the first one

That would be the following rule:

RewriteEngine has to be on for the rewrite rules:

Code:
RewriteRule ^my-blog-library/ new-blog-library/ [L,R=301]

Keep in mind that you don't add the first slash, since adding it will not meet the rule when accessing the url.

for the next one:

For specific URL's you have to keep in mind that ur using regex here so you need to close the url with a $, like this:
Code:
RewriteRule ^specific/url(/)?$ new-specific-url/ [L,R=301]

If you want the redirect to response with a 302 (temporary redirect) you simply change the 301 to 302

You can test your htaccess rewrites with the following site:

Code:
https://htaccess.madewithlove.be/
 
@jorun Thanks, mate!

And thanks to @Gogol and everyone else who helped / will help.

There's a ton to take in (again, for newbies like me, because for you this might be a second nature) but I'll figure them out eventually.

For now, it's enough though (my poor brain needs a rest), so we stop here. Cheers :)
 
Back
Top