Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite failing, simple redirect help

         

ErosWelker

7:44 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



I admit it, I'm a n00b. I've read three different tutorials, downloaded cheat sheets and tried to read the official documentation, but clearly, I'm just not smart enough. So help me please.

I'm working on a site that is migrating from ExpressionEngine to Wordpress, so its going to have the majority of its URLs changing. I want to redirect them instead of leaving them empty.

So basically I want pages like this:
[domain.com...]
[which will no longer exist post migration]

To redirect to pages like this:
[domain.com...]
(which will exist as a new template after I figure this out!]

Note the underscores versus hyphens. I've tried this literally hundreds of times and gotten nowhere. Other mod rewrites I've implemented have worked, such as redirecting nonsense URLs to the home page. I've disabled all of them to get this one trick to work though, and I'm failing left and right.

Here's what I'm rolling with currently:

Code:
RewriteRule ^/([A-Za-z0-9-_]+)/?$ index.php?/site/more/$1 [NC,L]

I'm sure I'm doing something wrong, and I'm hoping someone can point me in the right direction, or help me outright.

Thank you in advance.

jdMorgan

1:12 am on Aug 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your attempt looks functionally backward, in that it rewrites the "new pages" URLs to the old-style URL (with a query string), rather than what you said you wanted to do.

RewriteRule works like this:
RewriteRule ^pattern-matching-the-client-requested-URL$ new-URL-or-filepath-here [flags]

You'll need to use a RewriteCond to capture the query string, then back-reference that captured value in the RewriteRule's substitution URL as "%1". For a good start, see the references cited in our Forum Charter and the articles in our Forum Library about changing dynamic URLs to static URLs.

Jim

ErosWelker

10:15 am on Aug 7, 2009 (gmt 0)

10+ Year Member



LOL. I originally had it reversed, but then some other tutorial instructed me the other way so that was the last state I left it in. I'll try reversing again and seeing what happens.

But it sounds as if I need an additional line of code around this for that RewriteCond. I'll look that up, but thank you so much for your response.

One question on this though, does it matter that there will already be physical pages existing at the new location?

jdMorgan

1:56 pm on Aug 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does it matter? Well, yes it does, because you'll get a 404-Not Found if any non-existent resource is requested, whether that resource request has been rewritten or not.

Take it in steps if these files don't exist yet: Rewrite the old-URL requests initially to something that you know *does* exist, and get the query-string-plus-old-URL-path pattern match part working. Then once that works, create a few existing files to point the rewrite(s) to, and test that... divide and conquer.

On the RewriteCond subject: Look up RewriteCond in the Apache mod_rewrite doc. Then scroll down to the list of variables it can be used to examine, and you'll find %{QUERY_STRING}. Check the requested query string with a RewriteCond pattern, check the URL-path with the RewriteRule pattern, and if both match, do the rewrite using the 'pieces' of the query string and URL-path captured in the regex patterns to 'build' the new path as needed using back-references to $1-$9 and %1-%9, as needed and as documented.

We'll be happy to answer specific, focused questions here. It's just that there are not enough contributors with enough free time to accept 'write my code for me' requests... That can quickly get out of hand (and has done so in the past). Also, we do you no favors by posting a solution and saying just "Here, use this," since that will leave you with no recourse if the code is wrong, does something unexpected or unwanted -- except to come back and ask another question. Better you should understand your own server configuration, because that is what this code is... it's a server config modification, and one little typo can have far-reaching effects. Before using a chain-saw, a shotgun, or an F-22 fighter plane, it's advisable to read the instructions... :)

Jim

ErosWelker

1:55 am on Aug 8, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Thanks a lot for your patience and your advice. Unfortunately, this world is very foreign to me, and a bit frustrating since the documentation is very complicated. I've "read" it a few times now, and I'm willing to bet that F-22's manual is more comprehensible than the code docs I've seen. ;) I tried following your examples and spent all day again seeing what worked, and mostly what didn't.

Unfortunately, all my attempts have failed and I can't afford to work on something like this for days on end with no movement, so I'm giving up. You win this battle, Apache, but not the war! I'll have to turn to the paid experts to get this problem solved, and I understand this is not what this forum is for so will direct that request elsewhere.

Just wanted to say that I did appreciate your responses and attempt to help. I realize it's still not ideal, but once I see an actual working version of what I'm looking to do, then I can better understand it. As of now, there seem to be no examples of that anywhere so, I'm stuck. heh... but thank you again and good luck with the site!

jdMorgan

2:29 am on Aug 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The purpose of this forum is not to provide a free code-writing service, but rather to discuss Apache and Apache-related issues.

However, posting your best-effort code and asking a specific question about it certainly qualifies as discussion, so feel free to do so...

Jim