Best way to redirect old traffic in this case

Hey there guys. I have a website which is now “closed”. Now all the old URLs show a 404 page. I have now only an index.php file in root, where I explain why it’s closed. What I need: If you get a 404 you should be redirectet to the index file. What would be the best bet in this case?

I tried creating an .htaccess with this content:

ErrorDocument 404 /index.php

But now I get this:

http://www.forumapicoltura.com/29-il-calendario-dell-apicoltore/0

The URL remain the same and I get the content (with no style) from that index.php file.

Hi Norman

Try changing the href=“bluebliss.css” to href=“/bluebliss.css”.

1 Like

Thank you so much, gandalf458! I’ll use this techinque, but what if I wanted to redirect them all to the index.php file without mantaining the current URL? (just to know)

1 Like

You mean if you weren’t to use the 404 redirection? In that case you would need modRewrite. I’m not very good with modRewrite. I’m sure someone else can help there…

1 Like

Yes, basically I’d like to know which other options I could use in this scenario and how, just for reference - so maybe in the future I could use them. :smile:

Norman,

Your use of the ErrorDocument is fine but doesn’t give anything but the index.php file which may be out of the requested subdirectory. Since it seems you need a bit more help then ErrorDocument provides, let me recommend mod_rewrite (I assume you’re using an Apache server).

Your old file example did not show what type of files you’re serving (.html, .php, etc.) and the first reply suggests that your index.php needs an external css file.

[code]# locate in .htaccess file in DocumentRoot

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .? /index.php [R=301,L][/code]

The RewriteEngine directive merely ensures that mod_rewrite is not in the comment mode.

The RewriteCond is trying to match the NON-existence of the requested file (I’ve omitted the same test for directory; not necessary as Apache will ultimately look for the DirectoryIndex of a directory).

The RewriteRule will match anything and redirect to index.php (in the DocumentRoot) and display that as a permanent redirection.

Easy-Peasy!

If you would like to learn more about mod_rewrite (including examples), http://dk.co.nz/seo.

Regards,

DK

2 Likes

@dklynn The only thing is the OP is redirecting to a different domain. I was wondering whether RedirectMatch would be appropriate in this case?

Hi Gandalf!

I didn’t catch the domain change but it looked like the OP needed to 404 all requests to his index.php for the explanation (and possible link to … whatever). If that’s the case, he does not need to match every file request, simply redirect to index.php.

If that’s the case, Redirect may work but it may also change the subdirectory level (if not 301’d) which would interfere with any supporting (css, jpg, etc.) files.

Regards,

DK

1 Like

Thank you @dklynn, I think that the one you posted is even better. I’ll try it!

What if at this time I’d like to add specifil URL rules? Now I have/tried this:

#ErrorDocument 404 /index.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .? /index.php [R=301,L]

RewriteRule ^www.forumapicoltura.com/29-il-calendario-dell-apicoltore/0$ http://www.klayz.com/community/3186-il-calendario-dellapicoltore.html [R=301,L]

But if I go here: www.forumapicoltura.com/29-il-calendario-dell-apicoltore/0 I’m still redirected to the index page. Maybe I should move the rule up?

It’s worth a try, and FWIW I think it’s the right answer :slight_smile:

1 Like

Mhhh, nope. I moved it up and so now I have…

#ErrorDocument 404 /index.php

RewriteRule ^www.forumapicoltura.com/29-il-calendario-dell-apicoltore/0$ http://www.klayz.com/community/3186-il-calendario-dellapicoltore.html [R=301,L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .? /index.php [R=301,L]

But if I go here: www.forumapicoltura.com/29-il-calendario-dell-apicoltore/0 I’m redirected to the index.php file.

You have put it before you turn the rewrite engine on. However, I think it needs to be “Redirect permanent” rather than “RewriteRule”

Fingers crossed…

1 Like

Nope, I tried moving the Rewrite engine on rule up as needed but it doesn’t change…
So basically now the situation that I want is this:

REDIRECT ALL URLs to the index.php file
BUT REDIRECT X, Y, Z to specific URLs.

There’s a good chance that if you had taken the time to check out http://dk.co.nz/seo you would have found an example of what you need, If not, there’s a good chance @dklynn would add it

In any case, an htaccess file isn’t something you should be “experimenting” with when you don’t understand what to do and how to do it.
Poor regex can produce unexpected behavior.
A simple typo can bring the site down and result in HTTP 500 errors

Thanks, Alan. However, it looks as if Norman might be a “script kiddie” who has no interest in learning. Otherwise, why would he reference the DOMAIN in HIS RewriteRule?

Amen!

As all staff, I was (and still am) willing to help members to learn but I have always declined to “code for free” in response to “script kiddie” requests. This thread has begun to “smell” like that … but, Norman, I’m still here if you do want to learn!

Regards,

DK

Excuse me so much, guys. I really want to learn, I’m serious. I sincerely completely forgot to check the link you provided for me (http://dk.co.nz/seo) yesterday. When you posted it I really had no time to open the webpage due to work and then I completely forgot it, so I went ahead with the questions! I’m so sorry.

What do you mean? I didn’t get it (excuse me but I’m also fom Italy, and despite all I’m still learning english, so sometimes I need a “second clarification”). I know that this could be stressfull sometimes, for both. Did you mean that is bad to cite a real domain when you’re “asking” like I was?

Anyway, forumapicoltura.com is a simple website where I had a simple forum with just let’s say 10-15 posts, so I moved/closed it and I really don’t have so much traffic coming to it, so I’m not for example under the risk to lose traffic or whatever. So I started “script kidding” (if this is the right term) with it just to found a solution to do some redirects using the .htaccess file, as I never studied it in deep, and I toke advantage of this situation to post here in order to ask you what I asked. I know I should have studied it before asking, so I already knew what to do, but then in this case… I wouldn’t have posted here… I don’t know. Just clarify me some points when I really should post something here on the sitepoint.com forums, so I better understand and I’ll take care of what you’ll tell me for the future.

Anyway, thank you again fo your replies and excuse me for this long post! :smile:

Hi Norman,

Thank you for the quick response. I’m very happy to help you because helping members is what this forum has always been about. Please take several minutes to read the tutorial as I’ve had reports from MANY members that it has helped them (to the point that they claim to have bookmarked it to return for information as they needed more help). If you have any question about that LOoooooONG tutorial, please e-mail (I may need to update that page to clarify things for everyone).

I’m quite sure that your English is far better than my Italian (I could not tell that you were not a native English speaker). I’ve always told others that their fluency in English was far better than my fluency in … any other language!

One of the primary things about mod_rewrite is that the regular expression (regex) in a RewriteRule can only try to match the %{REQUEST_URI} variable; to match the {HTTP_HOST}, {QUERY_STRING}, etc., you MUST use a RewriteCond, specify the variable (and, possibly any other additional string) then the regex being used to test for a match. Thus, your

RewriteRule ^**www.forumapicoltura.com**/29-il-calendario-dell-apicoltore/0$ http://www.klayz.com/community/3186-il-calendario-dellapicoltore.html [R=301,L]

can NEVER match. Of course, that was one of the first things in my tutorial (after configuring your test server for mod_rewrite) so it was obvious that it had not been read.

The second thing was that I recommend that you verbalize the specification for your redirection(s) as doing so will allow you to hear the code needed to implement the redirection(s) … including the exclusions (where did X, Y, and Z come from?).

Please don’t worry about the length of your post. I know that I get verbose (too words) with some of my replies as I believe that it’s important to explain what the code is doing (see the examples in my tutorial, too). It’s all about communication so use whatever length post you need (there is no cost for the bytes used ;-D )!

Okay, if you would please clarify your specification (redirect everything except X, Y, and Z - please specify what these “files” are by name) to … is it to the new domain (same filenames as requested OR to the old domain’s index.php - with or without a query string, i.e., request=OldFileName). I call this “Specificity” and believe that it’s critical to creating accurate mod_rewrite code.

Despite your admission to asking a “script kiddie” question (’ just give me the code - I don’t want to know what it does’), please accept my apologies for suggesting that’s what you are. There is a major difference between “do it for me” and “help me learn” and I believe you have learned the lesson that, as a webmaster, you MUST know what your code is doing.

Regards,

DK

1 Like

Thank you a lot, @dklynn, first for your post, second for your compliments about my English (I have also to thank Google Translate a lot). :slight_smile:

At the moment, sincerely speaking, I really don’t know what %{REQUEST_URI}, {QUERY_STRING}, etc. does and how they work. So indeed I’ll need to first understand these things. You know, sometimes it seems that someone is asking something like “code it for me”, even if it’s not the case, and in this case of mine, my request was… yeah, probably purely like that, but with a second intention by myself to approach the thing in the folllowing way or manner: “Ok, if I will get some help I’ll certainly learn from it, whatherver it is. I’ll implement it. - It works? Great! I say thanks and I go on. Then I try to understand how it works - Two days later I have to do the same thing, I reuse the copied and pasted code and then I learn from it how it works, when to use it, etc.”. So maybe not always in the moment I use it. I think that you know better than me that sometimes it’s simply usefull to copy-paste and go on with what you like to do. I mean, without studying at 100% everything that you do.

Obviously there is a point where you have to stop doing and start re-learning/learning what you have / what you did. As an example, I did the same with my English knowledge: I started posting on english forums when I was able to sum up something like two - three words max. and nothing else. Then somebody told me: “Hey, what the hell did you wrote!? Please write it in a correct English or we’ll never understand”. From that point I slowly learned to sum (in a correct way) more than two - three words, create correct phrases and so on! Despite that, I could say that I’m still on a 50% of the English language learing process. :stuck_out_tongue:

Ok, in this case, as I have no real urge to create these rules (as said I’m not on a critic point where I’ll possibly lose a lot of traffic), I’ll skip from posting this now, at least before I’ll have read and understand the linked tutorial.

I do not agree here. ahahah … just kidding! LOL

Thank you again, dklynn. It’s always a pleasure to find and talk to people like you.

HI Norman,

It gives me a “cheap thrill” to know that I’ve helped someone “turn the light on” so they can go off and help someone else with the same thing.

Oh, my! Apache variables are available to mod_rewrite (Apache.org has a number of them listed in their mod_rewrite documentation) but the ones you’re asking about are VERY BASIC to every URL (Uniform Resource Locator - web page address) and every webmaster should be intimately familiar with them.:

URL = Web page address, e.g., http://www.datakoncepts.com/seo.php?query=nothing+special

The protocol is the http:// which signifies that you’re asking for an Internet web page. The protocols for web pages are either http:// or https:// (secure/encrypted). The https:// protocol is denoted by %{HTTPS} and is either on or null.

The domain is datakoncepts.com; the subdomain is www and, in today’s context, www is generally not necessary (depending upon how the domain is registered on the host … in its A Record; but don’t worry about this now).

The URI (Uniform Resource Identifiers) are everything after the domain name up to but NOT including ? or # (reserved characters). Above, it’s seo.php

The query string follows the “marker” (? merely separates the URI and the query string). The query string is normally made up of key=value pairs (separated by &'s). If the value string contains spaces, they’ve been replaced by +'s (because spaces are reserved characters and not allowed in a URL).

The other separator “marker” is the # which denotes that the following characters provide the name of an “anchor” in the URI’s page (so long pages like my seo can redirect to a specific location in the page rather than scroll endlessly looking for the appropriate location).

In short: {protocol}{domain}/{URI}?{query string}#{anchor}

or, using Apache variables (they’re normally denoted with a preceding % to let Apache know to look at its variables):

http:// %{HTTP_HOST} %{REQUEST_URI} ? %{QUERY_STRING} http:// datakoncepts.com /seo.php ? query=nothing+special

I do not believe the page anchor is available to mod_rewrite but can check on that if you like.

It’s far easier to provide for exclusions in your redirections (the X, Y and Z) when creating the redirection rather than when going back and modifying. To provide an example, let me assume that your pages are all .html pages and X is contact.html, Y is about.html and Z is location.html. Okay, it’s JUST an example for you to consider!

[code]RewriteEngine on
# Makes sure that mod_rewrite is NOT in "Comment Mode"
#
RewriteCond %{HTTP_HOST} old\.domain$ [NC] 
# ONLY necessary if old.domain and new.domain are sharing the same space on your server
# Where old.domain is like my datakoncepts.com
# The \ is so that the . (dot) character will only match the dot character not one of ANY character
# The No Case flag ( [NC] ) is required because the {HTTP_HOST} is NOT case sensitive
#
RewriteRule !^[contact|about|location|index]\.html$ /index.html?request=%{REQUEST_URI} [R=301,L]
# To redirect all pages EXCEPT contact.html, about.html, location.html and avoid looping on index.html
# Escape the dot character in the regex so it will only match the dot character
# Redirect by sending EVERYTHING ELSE to index.html
# Optionally add a query string denoting the request using the requested URI as its value
#
# OR 
#
RewriteRule !^[contact|about|location]\.html$ http://new.domain/ [R=301,L]
# Do ***NOT*** use both RewriteRules as only one should apply 
# - the first will take precedence
# Comment out or delete the one you do not want to use![/code]

If X, Y and Z had not been specified, they would have received PERMANENT redirections to new.domain (too late to retain at old.domain).

There’s a lot to learn about mod_rewrite but it is an exceptional tool in the hands of a knowledgeable webmaster. Take the time to learn (sufficient motivation should be provided in checking out the examples on the tutorial page).

Regards,

DK

1 Like