Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite on multiple files

Changing dynamic to static links

         

adamc

1:24 pm on Jun 29, 2008 (gmt 0)

10+ Year Member



I have read the guide on the RewriteEngine, and got a little confused, so thought I would post something about what I was trying to do, and see if anyone could help.
I have various urls, like viewArticle.php?id=x, users.php?u=x and category.php?c=x
What I would like is say, for the viewArticle to become article/x, users become users/x and category become category/x.

Would I also be right in saying, that if my viewArticle.php page contained forms, and links to it in the format viewArticle.php?id=x that the code would need to be re written in the format article/x, or does Apache take care of this, so if I didn't change to code, so say for example in article/34, I clicked on submit, where it's action was viewArticle.php?id=34, the url the visitor would see would remain article/34?

jdMorgan

3:48 pm on Jun 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the page-generation code would need to be modified to output on-page links in the format "/article/x".

The URL is defined by links appearing on your pages (and the pages of other Web sites). Apache mod_rewrite cannot change your page content, all it can do is to 'map' incoming requests for URLs (coming as the result of a click on one of your links) to the correct files on your server -- In this case, take a request for "example.com/article/x" and map it to "/viewArticle.php?id=x".

This is generally not hard to do, requiring only one line to be added to the code that generates your on-page links. Alternatively, you could mass-edit the database, so that it contains the new static URLs, and let the script just output them.

For more information, see this thread: Changing Dynamic URLs to Static URLs [webmasterworld.com]. There are additional tutorials in our forum library, and links to resources in our forum charter.

Jim

adamc

5:29 pm on Jun 29, 2008 (gmt 0)

10+ Year Member



Righty ho.

I have created a .htaccess in the folder with the viewArticle.php file, and using a rather simple rule did this:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^viewArticle.php?id=([0-9]+)$ /article/$1/ [R]

It doesn't work, like [another] article said it would. (I viewed yours, but I found this a tad easier to understand). I'm not understanding the logic of it all, is what I think the problem is.

[edited by: jdMorgan at 6:38 pm (utc) on June 29, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

6:37 pm on Jun 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please re-read the cited tutorial; Your rule is entirely backwards.

Pay attention to the sequence of events as a URL is rewritten, from click to content-deleivery.

Jim

g1smd

7:39 pm on Jun 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, that rule is a redirect and generates a 302 redirect.

To avoid Duplicate Content, you'll need that redirect (but you need to change it to be a 301 redirect) as well as adding code for the original rewrite that you wanted here.

The rewrite runs in the reverse direction to the redirect.

jdMorgan

9:30 pm on Jun 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The rewrite runs in the reverse direction to the redirect.

That is not at all what I meant. The rule attempts to externally redirect from a dynamic URL to a static URL, when what in fact is needed is to change the links on the page to static URLs, and then internally rewrite from these new static link URLs to the dynamic filepath needed to invoke the script(s).

Notwithstanding the facts that the rule won't work as written because RewriteRule cannot 'see' query strings, and that it attempts to generate an external redirect, the rule is backwards, in that it attempts to redirect from dynamic to static. Since the static URL will never resolve to an existing file, it cannot work.

Jim

adamc

6:18 am on Jun 30, 2008 (gmt 0)

10+ Year Member



Have this:

# Enable mod_rewrite, start rewrite engine
Options +FollowSymLinks
RewriteEngine on
#
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^article/([^/]+)/?$ /viewArticle.php?id=$1 [L]
#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewArticle\.php\?id=([^&]+)\ HTTP/
RewriteRule ^viewArticle\.php$ /article/%1/? [R=301,L]

And it still won't work. Any ideas?

jdMorgan

2:18 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please define "won't work" -- it's meaningless, because it could mean any of a hundred different kinds of problems...

Also, comment-out the second rule (or delete it) until you get the first rule working.

What you have there actually looks OK, except the the substitution URL in the second rule (only) should start with "http://" and your domain name. However, that's not the likely coause of your trouble.

Please describe how you tested and what your results were. Then state how those results differed from your expectations.

Also be sure to completely flush your browser cache after any change to your config code.

Jim

adamc

5:55 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



OK. Done the commenting out.
By not working, I mean that I still get directed and can see viewArticle.php?id=x and cannot go to /article/x

jdMorgan

10:32 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I still get directed and can see viewArticle.php?id=x

Sorry, I don't understand "I still get directed" at all.

By "can see viewArticle.php?id=x", do you mean that you see that page's contents, that URL in your browser address bar, or?

Did you change the link on your page to /article/x? Did you flush your cache before testing?

Jim

adamc

6:08 am on Jul 1, 2008 (gmt 0)

10+ Year Member



Yes and yes.

The problem was actually the / in front of viewArticle

Now, though, I am in a subdomain, and down two folders deep, and ofcourse my index link, or stylesheet link (in an SSI) don't work (due to the wrong filepath). Is there a way I can correct these links, without damaging them on other pages getting them called on the SSI?

jdMorgan

3:31 pm on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's the same problem in a different guise, and the solution would be the same: Correct the problem URLs (change them from page-relative to server-relative URLs), or detect and correct the invalid URLs using mod_rewrite.

Jim

g1smd

11:16 pm on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you begin internal URLs with "/" and therefore have them "count from the root" the problems will no longer appear.