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.
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:
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).
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/**)
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.
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:
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!
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!
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?
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>?
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?
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?
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).
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
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.
Bummer, what’s the REAL specification, if you please? Your latest post suggests ?page={whatever} => DocumentRoot and ?category={something to be retained} => duh, where?
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.