homepage Welcome to WebmasterWorld Guest from 54.198.130.203
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirecting to Rewritten URL
I changed my URL but need to redirect from old variable
GF_Diablos




msg:3740461
 2:26 pm on Sep 8, 2008 (gmt 0)

Hey guys, looking for a little guidance with relation to redirecting urls from their real to rewritten version.

Situation is this. I have a simple CMS on a site that generates article pages in PHP variable format. i.e. www.example.com/article.php?id=14 I used this redirect to make the URLs nice.

Options +FollowSymLinks
RewriteEngine on

RewriteRule article-id-(.*)\.php article.php?id=$1

Problem is that I need to redirect the old pages (article.php?id=14)to their rewritten variants.

Can anyone help me here please?

[edit]

I did read the library thread on this and came up with this but it has not worked, I guess because I messed up somewhere.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=(.*)\ HTTP/
RewriteRule ^index\.php$ http://example.com/article-id-(.*) [R=301,L]

[edited by: GF_Diablos at 3:08 pm (utc) on Sep. 8, 2008]

 

jdMorgan




msg:3740484
 3:24 pm on Sep 8, 2008 (gmt 0)

Several problems... Try this instead:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.com/article-id-%1 [R=301,L]

Check out the mod_rewrite and regular expressions documentation cited in our forum charter; Be sure you understand how this works before using it.

Jim

GF_Diablos




msg:3740517
 3:47 pm on Sep 8, 2008 (gmt 0)

Hmm I tried this but my dynamics are still not redirecting. The rewritten urls are there and working but dynamics still exist.

I am not sure exactly what is causing the problem, especially since you corrected my errors. Although I see my main problem, is I got the wild card and variable things confused.

I will try to get a bit more handle on the rewrite rule stuff as it is obviously this I don't understand but any ideas you can suggest why my site would break when trying to implement this would be great.

jdMorgan




msg:3740927
 11:57 pm on Sep 8, 2008 (gmt 0)

Your site breaks? How?

In general, the URL-path patterns in RewriteRules must exactly match the URL-path specified in the requested URL, but with the path to the current .htaccess file stripped. That is, if the requested url is example.com/foo/bar.html, and the rewriterule is in /foo/.htaccess, then the pattern in the rule must be "^bar\.html$" -- The rewriterule in the .htaccess file located in /foo will not see the /foo path-part, because it has already been stripped out by Apache.

Most failures are caused by incorrect patterns; If the pattern does not match the requested URL, the rule is never applied.

Also, be sure to completely flush your browser cache before testing any changes to your code; Stale page responses cached by your browser can invalidate your test results, since the browser will serve the page from its cache (in your computer) instead of sending a request to your server.

Jim

GF_Diablos




msg:3741041
 7:11 am on Sep 9, 2008 (gmt 0)

It's definitely not the cache, I have tried that already, the confusing thing is i'm pretty sure I understand the rewriting on the rules, I spent a good few hours last night reading about this more and still cannot see why it will not redirect.

I have one more question (thanks for putting up with me ehre) on what I don't understand in our example.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.com/article-id-%1 [R=301,L]

Why is the condition /index\ and not /article\

GF_Diablos




msg:3741050
 7:39 am on Sep 9, 2008 (gmt 0)

OK small update, after noticing I did that index and article thing wrong the redirect is apply but is not removing the dynamic cariable from the end.

So a redirect from www.example.com/article.php?id=1 is going to www.example.php/article-id-1?id=1.

I'm nearly there for all of my floundering, what is a common cause of this issue?

jdMorgan




msg:3741306
 3:42 pm on Sep 9, 2008 (gmt 0)

You want to check the client (browser) request to make sure that index.php was requested by the browser itself. The index.php URL-path tested by the pattern in the RewriteRule itself *could* have been changed as the result of your static-to-dynamic internal rewrite. Therefore, you must check both the current URL-request (in RewriteRule) and the original client URL-request (in RewriteCond) to be sure that this rule will only redirect from dynamic to static if the client requested the dynamic URL. If this is not done, then this rule and your original static-to-dynamic rewrite would countermand each other -- causing an "infinite" loop.

Again, the main cause of such problems is that the patterns in RewriteCond and RewriteRule do not match the requested URL-path and/or query string. These patterns must be exactly precisely correct, or the rule won't run. And again, remember that the URL-path tested by RewriteRule is "localized" to the .htaccess file in which the RewriteRule is located -- the URL-path-part that matches the .htaccess file's current location is removed from the URL-path tested by RewriteRule. Only RewriteCond will see the full requested URL-path.

You might want to back off and simplify: Try writing a rule in the form above that matches one and only one specific URL-path and query string, and redirect it to Google or some other known site. This eliminates the complexity of the back-references (variables) and also eliminates the possibility of looping on your own site. The URL-path you specify can even be a "made-up" (non-existent) URL, so that testing does not interfere with the normal operation of your site.

As an example:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /testrewrite\.php\?id=14\ HTTP/
RewriteRule ^testrewrite\.php$ http://www.google.com/? [R=301,L]

If you request example.com/testrewrite.php?id=14 with your browser, you should land at Google.com

If that does not work, then your RewriteCond is likely missing part of the URL-path info needed to get to the directory in which this .htaccess file is located. Or, there's another rule or directive (in this .htaccess file or one above it, or in the server config files) that is interfering with the execution of this one. There are several Apache modules with directives that can interfere: mod_alias, mod_proxy, mod_dir, mod_negotiation, etc.

You might also want to check your server error log -- The information there might prove very useful.

Jim

GF_Diablos




msg:3741335
 4:10 pm on Sep 9, 2008 (gmt 0)

Re-starting and working from the static example was the advice I needed, thanks very much for your help jdMorgan I actually have it working.
Hopefully I can take this struggle away and master this stuff now.

jdMorgan




msg:3741338
 4:17 pm on Sep 9, 2008 (gmt 0)

So what was the problem?

Jim

g1smd




msg:3741806
 7:37 am on Sep 10, 2008 (gmt 0)

The problem (I am guessing) is that there was no "index", it was "article" all the way.

g1smd




msg:3741813
 7:41 am on Sep 10, 2008 (gmt 0)

*** OK small update, after noticing I did that index and article thing wrong the redirect is apply but is not removing the dynamic variable from the end. ***

Try this for line 2:

RewriteRule ^article\.php$ http://example.com/article-id-%1? [R=301,L]

Adding the ? stops the query string being re-appended.

jdMorgan




msg:3741969
 1:37 pm on Sep 10, 2008 (gmt 0)

Yeah, I forget that almost every time -- Although I did include it in the "test" rule above... :(

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved