Forum Moderators: phranque
Here's the scenario, pretty basic.
I have www.example.com/viewthing.php?thingid=123
I'd like to make it look like www.example.com/thing123
I have this much working, using this:
RewriteRule ^thing([0-9]+) viewthing.php?thingid=$1
Now, I've noticed that when I put a slash at the end of my request, the page comes up, but the stylesheet does not load with it. What am I missing? The stylesheet is in www.example.com/includes/stylesheet.css
Also, how would I write that rule if I wanted to be able to type in www.example.com/123 instead?
Lastly, is there a way to make all requests for the dynamic url to be rewritten to the static one such that a request coming for viewthing.php?thingid=123 is shown in the url bar as /thing123? I'd rather not rewrite all of my code to link to the static url, and the site already has many links coming in to the dynamic url. I'd like the engines to stop using the dynamic url, so I want to feed them the static one even if they request the dynamic one. Hopefully that makes sense.
Thanks for your time.
> Also, how would I write that rule if I wanted to be able to type in www.example.com/123 instead?
Very bad idea. You will create the potential for dangerous "URL collisions" in the future. For example, what if the "Thing" has the same name as an existing directory or file? You can use any prefix (as in "/thing") you like -- even a single-letter prefix will reduce the chances for collision dramatically. It also makes it much easier to write unambiguous rewriterule patterns.
> Now, I've noticed that when I put a slash at the end of my request, the page comes up, but the stylesheet does not load with it. What am I missing?
You are missing the fact that it is the client (browser or robot) that resolves relative links, generating the canonical URL based on the lowest-level 'directory' that it 'sees' in its address bar. By adding a slash to the URL, you are saying that these relatively-linked objects are expected to exist in a directory below that implied if the slash is not present.
Solutions include changing included-object links to server-relative links, or adding mod_rewrite code to adjust the path of common shared objects. The former solution is to be preferred, because the latter creates 'duplicate content' -- the same content appearing to reside at multiple URLs.
Page-relative link: <img src="images/logo.gif"> (your current links)
Server-relative link: <img src="/images/logo.gif">
Canonical link: <img src="http://www.example.com/images/logo.gif">
Actually, it would be preferable to enforce strict control over your page linking -- either link with or without a trailing slash, and perhaps add a rule to force a 404-Not found if the page is incorrectly linked-to. Again, this is much easier to do if you use an unambiguous prefix for all "/thing" URLs.
Jim