Redirects

I took my best guess about where to post this…hope it’s the right place!

Wondering if someone can help with redirects/.htaccess files. I have followed several tutorials online to create these and they never come out correctly. I always have to email a friend and he has to fix them. Does anyone know a good tutorial for this?

This site was transferred from Geeklog to WordPress several months ago. Some of the pages from Geeklog are still showing in the SERPs and are still live so I am trying to redirect them.

Thanks so much in advance!

It depends a little on what you’re looking for. The basic 301 redirect (for when you’re re-named a file) is pretty simple:

Redirect 301 /old-file-name.html http://www.example.com/new-file-name.html

The above code should be self-explanatory. It is a bit tiresome if you have a lot of similar pages which you need to redirect. For this purpose, you can use RegEx redirects.

RedirectMatch 301 (.*)\\.aspx$ $1.php

The above code would be useful if the whole site has moved from ASPX to PHP, and all that you’ve changed is the extensions.

You can also combine them. In this case, the sequence is (or at least can be) important.

If you’re thinking about rewriting links, where a url named something like example.com/[b]some-page-name[/b] resolves into a GET query such as example.com/index.php?page=[b]some-page-name[/b], you can use RegEx too:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9\\-]*)$ index.php?page=$1

In this case, only the letters a through z (both upper-case and lower-case letters), numbers and the hyphen will work. It is important to note that while this seems to add a layer of protection, it’s still possible (and easy) to send any data using GET if someone can guess the variable name (in this case page). You will therefore still need to properly sanitize your input (or use prepared statements).

A better guess would be the Apache Configuration forum …

So, I just found out for Geeklog, all news stories have the url …/article.php/** and all static pages have the url …/staticpages/index.php?page=** (or with url_rewrite: …/staticpages/index.php/**)

**= story id/slug, page slug/id

It says, “Just make 2 redirect(match) rules to redirect them to the new permalinks, see http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirectmatch

Would the .htaccess file be:

redirect 301 …/staticpages/index.php/** http://www.subtleyoga.com
redirect 301 /article.php/** http://www.subtleyoga.com/category/blogging-towards-bliss/

Wait, woops. I have to make redirect match rules to redirect them:

RedirectMatch (.*)\\.gif$ http://www.anotherserver.com$1.jpg

I know this is not correct:

RedirectMatch (/staticpages/index.php/) http://www.subtleyoga.com
RedirectMatch (/article.php/
) http://www.subtleyoga.com/category/b...towards-bliss/

Don’t get it at all??? What do I do?

I’m not entirely sure what you’re trying to redirect where. Can you give a couple of specific examples?

I’m trying to make 2 redirect(match) rules to redirect all the /static pages and news stories to the new site. What it says is:

all news stories have the url …/article.php/**
all static pages have the url …/staticpages/index.php?page=** (or with url_rewrite: …/staticpages/index.php/**)

**= story id/slug, page slug/id

So, I was trying to redirect all the static pages to the homepage and all the news stories to the blog page.

Not sure if that answers your question.

Try this:

RedirectMatch 301 \\/article.php\\/(.*) http://www.subtleyoga.com
RedirectMatch 301 \\/staticpages\\/(.*) http://www.subtleyoga.com/category/blogging-towards-bliss/

Ohhhh! Although I don’t think I would have figured that out. Hope I can learn this stuff! Do I add it that to the bottom of the current .htaccess file which has:

## php_flag register_globals off
Options +FollowSymlinks

DirectoryIndex index.php index.html
ErrorDocument 404 /404.php

## php_value post_max_size "20M"
## php_value upload_max_filesize "20M"
## php_value max_input_time "120"
##php_value include_path /home/content/s/u/b/subtleyoga/lib/geeklog/ACTIVE-geeklog-1.5.0/system/pear

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Actually, this looks like it should be updated? Has Geeklog still referenced?

Scully,

It looks like CA has done a better job of following your posts than I have and provided RedirectMatch as a solution. My only criticism is that you do NOT escape the / characters in regex.

I believe that you are trying to redirect from an OLD site to a NEW site changing the URI which may be dependent upon the query string. If that’s the case, you need the power of mod_rewrite (see the tutorial linked in my signature).

For me to be able to help you, though, you’ll really need to create a “specification” which clearly demonstrates the different redirections which are required including the URI (path/file) and query string variables which need to be captured and sent along in the redirection.

NOTE: Since you’re using WP where all the (non “support file”) requests are handled by index.php which examines {THE_REQUEST} to determine which modules to use to fetch data and create the file, you must redirect in the manner WP is expecting.

NOTE 2: If you’re a webmaster, you should KNOW BETTER THAN to repeatedly ask Apache (ON EVERY REQUEST) whether you have mod_rewrite enabled: REMOVE the <IfModule> wrapper!

Regards,

DK

My only criticism is that you do NOT escape the / characters in regex.

You are, of course, correct. I usually use RegEx with PHP, where the forward slash has to be escaped. Thanks for spotting it :slight_smile:

CA,

No worries. AND / only needs to be escaped if you’re also using it as the delimiter for the regex string. I tend to use ~ for that as it’s not likely to ever show up in any text string of mine! :lol:

Thanks for covering for me while I slept!

Regards,

DK

dklynn,

I am soooo confused. I’m not sure how/what else to explain?? I’m trying to forward all the static pages and blog posts from old site on Geeklog to the new site on WordPress (the domain name didn’t change; still the same). Geeklog documentation says, “Just make 2 redirect(match) rules to redirect them to the new permalinks”. I think you read that static pages and the news stories can be redirected with one line each on .htaccess file. That’s what C. Ankerstjerne is trying to help me with. Isn’t what he did correct?

Examples are:

Move this page: http://www.subtleyoga.com/staticpages/index.php?page=asheville-yoga-teacher-training
to this page: http://www.subtleyoga.com/staticpages/index.php?page=asheville-yoga-teacher-training.

Move this page: http://subtleyoga.com/staticpages/index.php?page=asheville-yoga-teacher-training
to this page: http://www.subtleyoga.com/yoga-teacher-training/.

All the static pages on old site to new static pages on new site.

Same with any blog posts.

I didn’t write that .htaccess file. It was transferred over from person who set up Geeklog. No, .htaccess files are not my forte. So remove: <IfModule mod_rewrite.c></IfModule>?

Scully,

Confused and … well, duplicating your first example - I suspect that the “redirection” is to the non-index.php version like the second example.

Okay, same website, just changing the URI using RedirectMatch statements? Yes, it looked like CA was doing a good job helping you.

FIRST, you cannot send a link to a non-file (and non-directory) and expect Apache to serve anything other than its 404 page. If you are relying on WP to do that for you, you can’t redirect away from the index.php script!

Second, you REALLY should not be using code you don’t understand.

Third, the <IfModule> and </IfModule> bits should be removed, NOT what comes between them! If you don’t have the module, you’ll get a 500 error - that’s all the WP people were trying to save you from - they should include the warning to remove them to save abusing the server.

Now, I asked for a specification similar to /staticpages/index.php?page=asheville-yoga-teacher-training => /staticpages/index.php?page=asheville-yoga-teacher-training (sorry, that was your first example - see why I’m confused?) OR /staticpages/asheville-yoga-teacher-training. IF you are relying on the WP code to redirect to index.php and use {THE_REQUEST} to serve the page, that’s okay. That would be something like:

# BEFORE the WP code!
RewriteCond %{QUERY_STRING} ^page=([^&]+)$
RewriteRule .? staticpages/%1? [L]

This will only test the query string for a value for page then redirect EVERYTHING (with that query string to staticpages/{value of page} and the ? will KILL the query string to prevent looping.

Did I guess correctly at your “specification” and what you’re trying to do with this?

Regards,

DK

I didn’t know that - have been using the forward slash for years now :lol: Live and learn :slight_smile:

I agree. Is there an online tutorial or online class (like ed2go) out there that teaches this?

Also, I typed in the redirect on the first example wrong. I meant:

Move this page: http://www.subtleyoga.com/staticpage…acher-training
to this page: http://www.subtleyoga.com

Dk,

Thanks for those tutorials. I will definitely check them out b/c this is all Greek.

Who’s on first? I want all /static pages to go to the index page. I was just giving the teacher training page as one example. Is C. Ankerstjerne’s correct or the one you updated?

Yours
RewriteCond %{QUERY_STRING} ^page=([^&]+)$
RewriteRule .? staticpages/%1? [L]

C. Ankerstjerne’s
RewriteCond %{QUERY_STRING} ^page=asheville-yoga-teacher-training$
RewriteRule .? http://www.subtleyoga.com/? [R=301,L]

I was hoping for all blog posts (…/article.php/**) to go to http://www.subtleyoga.com/category/blogging-towards-bliss/) if possible too but static pages most important.

CA,

I believe that ^ is another “commonly” used marker for regex. To each their own, though, unless it gets in the way of your coding.

Mastering Regular Expressions by Friedl is the book to reference EVERYTHING regex. However, it’s a coder’s tome as a very limited subset really applies to mod_rewrite (because it only deals with URIs and other Apache variables).

Regards,

DK

Sculley,

Tutorial (for mod_rewrite) is linked in my signature. Tutorial for PHP is best in the user supplied comments at www.php.net. Tutorial for JavaScript is pretty much non-existent (at least I haven’t found any - jQuery has some bits and pieces and SitePoint does sell books from some JavaScript Gurus).

To remove the staticpages from the first redirection, you could use something like

RewriteCond %{QUERY_STRING} ^page=asheville-yoga-teacher-training$
RewriteRule .? http://www.subtleyoga.com/? [R=301,L]

which will redirect to your DirectoryIndex (which should be named in the redirection) and strip the query string with the ?. Because this really should receive a permanent redirection, I also added the R=301 flag.

Regards,

DK

Sculley,

Bummer, what’s the REAL specification, if you please? Your latest post suggests ?page={whatever} => DocumentRoot and ?category={something to be retained} => duh, where?

Okay, the first half is solved by

RewriteCond %{QUERY_STRING} ^page=
RewriteRule .? /[COLOR="Magenta"]index.php[/COLOR]? [L]

because you aren’t interested in the content of the query string, only that it contains page=. Personally, I HATE the direction to an unspecified file (which makes Apache go off and look for the DirectoryIndex so I’d added the index.php above.

CA’s code was using your example to send ONLY that query string to the DocumentRoot. In other words, you’ve confused us with your lack of a good specification.

Did I guess correctly this time?

Regards,

DK