views:

31

answers:

1

hello people,

OK I have been messing about with URL rewriting for the last few days and seem to have come to a bit of a dead-end. I have come up with a few solutions that work on some servers and not others, and my hosting company (1and1 - be vary wary of these guys if you choose them as hosts) hasn't been able to help at all.

My problem is this, i want to re-write this url:

/result.php?section=[section name]&url=[url]

to this:

/article/[section name]/[url]/ (adding a trailing slash if there is none)

and ALSO

section.php?section=[section name]

to

/section/[section name]/ (again adding a trailing slash)

each attempt seems to have different results. currently I am using thhe following, which works locally, but on the live server only the 'articles' rewrite works:

RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2

RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ section.php?section=$1

Can anyone help me come up with a solution that will work nicely? Thank you in advance, I am really struggling with what seems like something relatively straightforward...

+2  A: 

Firstly in order to force the backslash on the end you will need to actually do a redirect...

RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/ [R=302,L]
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/ [R=302,L]

Note: I use 302 because 301 can be a pain during testing, once everything is working and you are happy with it change it to 301.

The two remaining rules are the rewrites that simply mask/alias the query_string URL, which should look something like this...

RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ /section.php?section=$1
Cags
Cags - thank you so much for your help! the trailing slash problem is solved, but strangely the section rule doesn't work on the live server (fine on my local machine). Is there anything there that looks wrong in there, or could be tidied up?
james
My only real advice is to check the URLs the article rule redirects to a results.php page in your root directory and the section rule redirects to a section.php in the root directory. When you say it doesn't work what do you get? A 404 error? If so try adding a [R=302] flag to the end of the rule, the URL in your address bar should change and hopefully the error will be obvious.
Cags
thank you for the tip, that was very useful! so now i see that on the live server the redirect doesnt work at all. on my machine, /section/art/ redirects to /section.php?section=art, whereas the link on the live server just goes to /section/art/ with no redirect. Is there anything else I could try here?
james
Do you have a different browser on your computer to test it with? I've had a lot of problems with cached information in the past. Failing that try hit ctrl+f5.
Cags
yeah i've been using ff/chrome/ie etc, always the same issue sadly. I'm thinking there must be something wrong with the rule itself?
james
how can there be it's essentially the same rules as the other one, do you have a link to a live example? Are there other rules in your .htaccess file? Is there a directory called section that contains stuff?
Cags
ok sure thing, here is an example live url: http://organdonor47.com/article/film/movie-review-a-prophet-2009/I can't put more than one URL in a comment, but the section links should work in the same way but don't, for example /section/film/the full htaccess file is in my next comment!
james
james
imagine that the htaccess file is all separated by new lines...
james
Ahh, I think it might be a content negotiation issue. If you rename section.php to something other than section it will probably start working. Note how article != result. The other way to fix it would be to add Options -MultiViews
Cags
oh, also, move the www RewriteCond and RewriteRule to above the other RewriteRules.
Cags
ok listen = Cags you are an absolute star. It all works just fine now, and i really appreciate all your help on this. I renamed the page and associated rule and it seems just fine. MY HERO
james
NP, don't forget to accept this answer as the accepted answer :)
Cags
james