Forum Moderators: phranque

Message Too Old, No Replies

URL Rewrite help -- newbie frustrated

URL Rewriterule

         

TMRBac

8:38 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



In a nutshell, I'm pulling my hair out trying to get URL rewrites to work. I've never done them before so bear with me.

Here's what I'm trying to do. I have a dynamic URL appearing on the homepage (index.php) of my site:

http://www.example.com/news.php?id=23

I want the URL to rewrite to look like this:

http://www.example.com/news/dynamicarticletitle/23

The dynamicarticlename is pulling from the database along with the article id number.

I've tried every which way using Rewriterule to get this to work but haven't been able to. I got it working once with just the id number and not the article name, but then the rest of the images on the site had their paths rewritten and were broken.

Here's the latest pass in .htaccess to get it working only with the id number, to keep things simple:

RewriteEngine on
RewriteRule ^news/([0-9]*)/$ news.php?id=$1 [L]

And here's the html:

<a href="news/id">

Simply enough. Any advice would be appreciated. I suspect this is a 10 second fix. Thanks!

TMRBac

9:03 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



A quick update:

Here's the code where I got the rewrite to work, but all the images are broken because its adding the rewrite URL to the images URL:

RewriteRule ^news/([0-9]*)/?$ /news.php?id=$1 [L]

jdMorgan

11:47 pm on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember that it is the browser that resolves relative links, and it does so according to the URL in the address bar.

You have a couple of choices: You can either change the links for included objects on your pages to server-relative or canonical form, or you can add a rule to rewrite the image requests to their actual locations. Examples of server-relative and canonical image links are <img src="/images/logo.gif"> and <img src="http://example.com/images/logo.gif">

Jim

TMRBac

2:41 am on Nov 30, 2007 (gmt 0)

10+ Year Member



I've got all the image paths set to their actual locations, both for images on the same domain and some housed on a sub-domain. Neither are appearing, so I'm assuming I'm going to need a rule to rewrite the image paths.

Know of any good examples I can go off of? I've looked around and haven't found anything directly applicable yet.

jdMorgan

3:17 am on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would be a bit difficult, since we have no idea what your "image paths" might look like...

Jim

TMRBac

4:14 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



Doh!

Here is an example on the same domain:

/images/example.gif

Here is an example on the main domain (the site I'm working off is a sub-domain):

[example2.com...]

jdMorgan

4:46 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, first question then is why do the image requests appear to get rewritten, because the URL-path "/images/xyz.gif" does not start with "news/" and it contains characters other than 0-9. Therefore it will not match your rule pattern and invoke the rule:

RewriteRule ^news/([0-9]*)/?$ /news.php?id=$1 [L]

So is there some other way you can interpret the error you're seeing? -- Anything useful in the server error log?

Jim

[edited by: jdMorgan at 4:47 pm (utc) on Nov. 30, 2007]

TMRBac

5:33 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



I looked through the error log (first I've ever done it, I'm a relative newbie) and couldn't find anything relating to that page.

BUT, I did play around some more and figured out that relative paths to images on the same sub-domain do, in fact, work. It's the images I'm calling from the main domain and another sub-domain that are not working.

I could shift image locations around and fix that issue, but if you know off the top of your head an easy fix example in .htaccess, I'd love to know it for future reference.

Thanks!

TMRBac

6:39 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



Nevermind, I got it working. Thanks for your help, though! I'll surely have another question here shortly as I try to expand the link to include the article title with characters stripped and the url encoded.