Forum Moderators: phranque

Message Too Old, No Replies

url rewriting with htaccess

         

mattpointblank

6:37 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



Hey all.

I'm having issues with .htaccess files to change dynamic PHP urls on my
page. Here's what I'm using:

RewriteEngine On
RewriteRule ^reviews/([0-9]+)/»
reviews/displayreview.php?id=$1
RewriteRule ^reviews$ index.php

Essentially I'm running this in /matt/dev/ (off root). I want to change
urls like /reviews/displayreview.php?id=$foo into /reviews/foo/ ..?
When I apply the above code I just get 500 errors.

While we're on the subject, I also want to redirect all urls to the old
system into the new one, eg:

[mysite.com...]

into:

[mysite.com...]
or even better:
[mysite.com...]

I've tried this several time after following tutorials and never
managed to succeed; can anyone help?

jdMorgan

7:52 pm on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mattpointblank,

Welcome to WebmasterWorld!

> When I apply the above code I just get 500 errors.

What do you see in your server error log when you get these errors? It will often tell you exactly what's wrong...

Jim

DeanC

9:35 pm on Nov 20, 2005 (gmt 0)

10+ Year Member




RewriteEngine On
RewriteRule ^reviews/([0-9]+)/?$ reviews/displayreview.php?id=$1 [L]
RewriteRule ^reviews/?$ index.php [L]

You mentioned in your description you wanted to rewrite /reviews/displayreview.php?id=$foo into /reviews/foo/. Well you can't do this as it is, id's aren't magically transformed into their corresponding db text. But i'm sure you can pick it up from here :)

mattpointblank

10:03 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



To be honest, my biggest concern right now is changing the old urls into the new ones (for the site relaunch). I can worry about more attractive URLs later, haha.

mattpointblank

12:56 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Dean:

I used that code and modified it for other pages, but got a weird problem.

Here's the code:

RewriteRule ^features/([0-9]+)/([0-9]+)/?$ features/displayfeature.php?id=$1&page=$2 [L]
RewriteRule ^features/?$ features/index.php [L]

This is the format of my links:

mysite.com/matt/dev/features/displayfeature.php?id=63&page=0

I tried accessing the url like:

mysite.com/matt/dev/features/63/0/

This displays the content and everything, but without a css style! Very odd. If I access the page with the above (lengthy) url it displays fine. Is this some relative path issue?

DeanC

3:15 am on Nov 21, 2005 (gmt 0)

10+ Year Member



You'll need to put a "rewritebase /" or possibly "rewritebase /features" below the rewritengine on line :) Sorry i'm not used to working with mod_rewrite outside of the directory I'm in ;)

mattpointblank

3:23 am on Nov 21, 2005 (gmt 0)

10+ Year Member



I tried both of those, they just stopped all the other redirects working, as well as not changing the problem :(

The weird part is, all the other links are in subfolders as well. Like, here's my structure:

matt/
matt/dev/
matt/dev/reviews/
matt/dev/news/
matt/dev/features/

The links for reviews and news both work fine, which doesn't seem to make any sense!

jdMorgan

3:28 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No answer for message #2?

If you can access your pages using /matt/dev/... then it sounds like a serious server config problem -- That full path should not be Web-accessible and I'd say that DocumentRoot is probably misconfigured. Do you run this server or pay for hosting?

Jim

mattpointblank

3:31 am on Nov 21, 2005 (gmt 0)

10+ Year Member



sorry Jim,

I checked the error logs and it gave a warning from mod_security, which once I disabled, set the .htaccess files working.

As for the path stuff, sorry, that was just me simplifying stuff. The full path is obviously a bit longer than that.

jdMorgan

3:38 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My point being that in most configurations, the full path is not Web-accessible, and should not be. Who runs the server?

(I sometimes ask a lot of questions before jumping into coding a solution. It avoids confusion, and also metaphorically, it avoids putting a band-aid on a major-trauma wound.)

Jim

mattpointblank

12:05 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



It's hosted by dreamhost.com. Once more let me stress that I can't access my site via paths, I just wanted to outline the directory structure of that specific folder.

jdMorgan

3:49 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Try 'rooting' the substitution URL-paths:

RewriteRule ^features/([0-9]+)/([0-9]+)/?$ /features/displayfeature.php?id=$1&page=$2 [L]
RewriteRule ^features/?$ /features/index.php [L]

Be aware that if you are using relative links on your pages to include CSS and images, then this may be the cause of the trouble. Remember that it is the client (browser) that resolves relative links, so it will still be expecting them to be in a third-level directory, such as /features/63/0/logo.gif if you use relative links.

As a result, you may need a specific ruleset to steer the CSS and image requests to the proper 'real' file locations... Depends on how you've set things up.

Another solution would be to make your links server-relative, rather than page-relative, i.e. <img src="/logo.gif">

Jim

mattpointblank

7:07 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Jim:

I tried that code, just gives a "No input file specified." error.

I am using relative links for css and stuff, but surely they should still work, as:

a) the same code works for the reviews page, which is in a subfolder off /matt/dev/ just like features

b) the url being /matt/dev/features/ (with a reference to ../style.css) shouldn't change anything, as the old url was just matt/dev/features/displayfeature.php

Really odd!

jdMorgan

7:12 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rules only apply to /features and its subdirectories, so I don't follow your deduction.

If the browser asks for /features/63/0/ and that page contains a link to <img src="logo.src", then that image will be requested from /features/63/0/logos.src. If you don't rewrite the image request or root it to your "Home page" directory, the request is going to fail.

Jim

mattpointblank

7:28 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Ohhh of course, I'm stupid.

The features stuff has a second value in the url (it corresponds to page numbers) so everything is being misrouted.. of course! Damn. At least that explains the issue. It'll be too much hassle to change all the relative paths (I php include the same header/footer for every site page, and my css) so I'll just not use url rewriting for the features section.

Thanks a lot for the help!