Forum Moderators: phranque

Message Too Old, No Replies

Rewrite help needed - this is beyond me

Rewrite help

         

mealybar

5:42 pm on Jun 5, 2005 (gmt 0)

10+ Year Member



At the moment I have individual folders and files all the same but for one thing, the 'club name'.

I'd like to have one file for each of the pages, for all clubs; but to make it look like they are still in folders.

bit like this:

url.com/clubs/example/index.php

>to<

url.com/clubs/index.php?folder=example

The pages are index.php, news.php, photo.php and results.php

but results.php would need another variable (for the result number):

url.com/clubs/example/results123.html

>to<

url.com/clubs/result.php?folder=example&id=123

This all way beyond me, all I've got going so far is for the results bit result123.html to result.php?id=123 code =>
Options +FollowSymLinks
RewriteEngine on
RewriteRule result(.*)\.html$ /clubs/example/result.php?id=$1

Cheers
Ric

jd01

8:00 pm on Jun 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ric,

It looks like you are on the right track... Thanks for posting an example and what you are trying =).

url.com/clubs/index.php?folder=example

url.com/clubs/example/results123.html

>to<

url.com/clubs/result.php?folder=example&id=123

You could use this: (if example does not change, change from the regex to the actual word example.)

RewriteRule ^clubs/([^/]+)/(index\.html)?$ /index.php?folder=$1 [L]
RewriteRule ^clubs/([^/]+)/result([0-9]+)\.html$ /result.php?folder=$1&id=$2 [L]

([^/]+) = 1 or more of anything, except a slash.
(index\.html)? = index.html is optional so either / or /index.html will qualify.
([0-9]+) = 1 or more of the number 0 to 9
\. = A litteral .(dot) not 'anything except the ending of a line'

I would think about putting the code 'deeper' in your site to eliminate some processing overhead. You could do this by creating the directory 'example', which I am assuming is a club name, then using an htaccess file instead of content, like this:

In the directory /clubs/example/:
RewriteRule ^(index\.html)?$ /index.php?folder=example [L]
RewriteRule ^result([0-9]+)\.html$ /result.php?folder=example&id=$1 [L]

The biggest difference is in the main htaccess file, you will have to process all the way to the first letter after the second slash, for each request before a rule can truly qualify or fail. In the second example, you are already in the correct directory, so you reduce your variables by one and you can qualify or fail a rule by the first letter.

Hope this helps.

Justin

Added: The benefits of the second example may increase/decrease based on the number of actual directories you would need to have.

mealybar

2:56 pm on Jun 6, 2005 (gmt 0)

10+ Year Member



Thanks for the reply =D

The first one =>

RewriteRule ^clubs/([^/]+)/(index\.html)?$ /index.php?folder=$1 [L]
RewriteRule ^clubs/([^/]+)/result([0-9]+)\.html$ /result.php?folder=$1&id=$2 [L]

([^/]+) = 1 or more of anything, except a slash.
(index\.html)? = index.html is optional so either / or /index.html will qualify.
([0-9]+) = 1 or more of the number 0 to 9
\. = A litteral .(dot) not 'anything except the ending of a line'

is exactly the thing I want. Yes example changes with every club too. You say ([0-9]) is 0 to 9, but there is potentially an infinate amount of results, any way to change that slightly?

Oh aye, I put this as a .htaccess file in the folder url.com/clubs/ but all I got were 404 errors. I did make a little addition to the htacces, but only where I had to rename files, here's my htacess that I tried =>

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^clubs/([^/]+)/(index\.php)?$ /clubs/clubindex.php?folder=$1 [L]
RewriteRule ^clubs/([^/]+)/(news\.php)$ /clubs/clubnews.php?folder=$1 [L]
RewriteRule ^clubs/([^/]+)/(photo\.php)$ /clubs/clubphoto.php?folder=$1 [L]
RewriteRule ^clubs/([^/]+)/result([0-9]+)\.html$ /clubs/clubresult.php?folder=$1&id=$2 [L]

Ric

jdMorgan

2:35 am on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ric,

Welcome to WebmasterWorld!

If you put the code in the .htaccess file in the "/clubs" directory, then you'll need to remove "clubs/" from the patterns in your rewriterules. The local-URL-paths "seen" by RewriteRule are localized to the directory in which that rule resides, so "clubs/" will be stripped out of the URL by the time the .htaccess rules in the "/clubs" directory see the URL. This is the meaning of "per-directory rewrites". Also, the second set of parentheses appears to be superfluous in the first three rules.

So, your first rule should look like this:


RewriteRule ^([^/]+)/index\.php?$ /clubs/clubindex.php?folder=$1 [L]

So, you're getting a 404 because the patterns never match and the rules are never being invoked.

Jim

mealybar

10:19 am on Jun 7, 2005 (gmt 0)

10+ Year Member



Thanks again, I think I'm still being an ideot though! I still get 404's. I see why the clubs/ is removed, but I didnt get the bit about parenthesis?

Just to maybe explain my problem again, as maybe I've not done it right before.

[url.com...]
>to<
[url.com...]

and

[url.com...]
>to<
[url.com...]

I'm trying to put the htaccess file in the clubs directory, is this correct? I dont think it makes alot of difference, other than my code would have to be slightly different.

mealybar

10:34 am on Jun 7, 2005 (gmt 0)

10+ Year Member



Sorry its official I am stupid..lol
Got it working 99% now.
Thanks a million guys =D

mealybar

10:45 am on Jun 7, 2005 (gmt 0)

10+ Year Member



OK last teathing problem.

I have a directory of photos, news, etc. within the clubs/ directory, these are going to be effected by the rewrite rules. Is there some way of exculding some directories from the usual rewrite?

Also if there is a trailing slash missing on

[url.com...]

a 404 comes up, will this code fix that problem:

RewriteRule ^([^/]+)$ /clubs/$1/ [L]

jdMorgan

1:20 am on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code in your Web root directory will fix any missing trailing slashes:

# Fix missing trailing slash on any URL which does not contain a period
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
Rewriterule (.+) /$1/ [R=301,L]

To exclude directories or file types, you'll need to construct an exclusion based on the filepath or filetype using RewriteCond and a negative pattern. The ruleset above demostrates this by testing for any local URL-path that does not end in a slash and does not contain a period. If both conditions are true, then the rule is invoked, adding a trailing slash.
Jim

mealybar

8:10 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



Thanks for the tips, I've been looking into it but dont seem to be able to get it to quite work. I think there is something simple that I'm over looking. My code is:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!^clubs/admin/ [NC,OR]
RewriteCond %{REQUEST_URI}!^clubs/intro/ [NC,OR]
RewriteCond %{REQUEST_URI}!^clubs/photos/ [NC,OR]
RewriteCond %{REQUEST_URI}!^clubs/news/ [NC,OR]
RewriteCond %{REQUEST_URI}!^clubs/unc/ [NC,OR]
RewriteCond %{REQUEST_URI}!^clubs/bicc/ [NC]
RewriteRule ^([^/]+)/(index\.php)?$ /clubs/clubindex.php?folder=$1 [L]
RewriteRule ^([^/]+)/news\.php$ /clubs/clubnews.php?folder=$1 [L]
RewriteRule ^([^/]+)/photo\.php$ /clubs/clubphoto.php?folder=$1 [L]
RewriteRule ^([^/]+)/result\.php$ /clubs/clubresult.php?folder=$1 [L]
RewriteRule ^([^/]+)/result([0-9]+)?\.html$ /clubs/clubresult.php?folder=$1&id=$2 [L]

Its the conditions at the start. Any requests to www.url.com/clubs/unc (or any of the others) I dont want to be processed by the rewrite rules below it. With this code I've found that the admin condition lets me access the admin dir, but the other condititons dont seem to work. Any ideas?

Many thanks =D

Edit: The code is in the htaccess of the url.com/clubs/ folder. Cheers!

jd01

8:41 pm on Jun 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ric,

I would try removing the OR from your condition flags. By using OR, the conditions will not qualify. Generally speaking most (90%) of the time when you use a ! and multiple conditions, you will want to use the implicit AND, not OR. I would try to explain, but the only way it make any sense to me is to say it as a sentence, out loud... if the condition is not this and is not this...

That's the only thing I see with a quick glance, please post again if this does not help... and define exactly what you mean by 'does not work'.

Justin

jdMorgan

9:08 pm on Jun 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Justin's assessment is correct -- don't use [OR] in this case.

Also, be aware that RewriteConds affect only the single RewriteRule that follows them. If you need those RewriteCond exclusions on all of your rules, you'll probably want to take a different approach.

Jim

mealybar

10:19 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



Thanks for your replies again. I've given it a try without the [OR]'s and still it is fine with the admin folder, it excludes that from the rewrite but the others (unc, bicc, etc) still try to load the clubindex.php.. etc pages.

I'm not sure what the REQUEST_URI brings back, so maybe its a problem with the clubs/bicc/ or bicc/ test for it?

I didnt know that the conditions only work for the rewrite after it; is there a way to have the conditions for all rewrite rules? or should I copy and paste them before every rewrite?

Sorry for all the questions :S

jdMorgan

10:47 pm on Jun 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, just handle the "positive" case instead of the "negative" case. Skip the rules if an excluded directory is detected.

Options +FollowSymLinks
RewriteEngine on
#
# Skip next five rules if excluded subdirectory
RewriteRule ^clubs/(admin¦intro¦photos¦news¦unc¦bicc)/ - [S=5]
#
# Else not an excluded subdirectory -- process the following rules
RewriteRule ^([^/]+)/(index\.php)?$ /clubs/clubindex.php?folder=$1 [L]
RewriteRule ^([^/]+)/news\.php$ /clubs/clubnews.php?folder=$1 [L]
RewriteRule ^([^/]+)/photo\.php$ /clubs/clubphoto.php?folder=$1 [L]
RewriteRule ^([^/]+)/result\.php$ /clubs/clubresult.php?folder=$1 [L]
RewriteRule ^([^/]+)/result([0-9]+)?\.html$ /clubs/clubresult.php?folder=$1&id=$2 [L]
#
# Skip=5 lands here to continue with any other following rules
#
... more rules ...

Notes:
  • You must always update the skip count if you add more rules.
  • If there are no more rules to be processed after this block, you can change [S=5] to [L] to simply exit there.
  • You must replace the broken pipe "¦" character with a solid pipe character before trying to use this example.

    Jim

  • mealybar

    1:57 pm on Jun 10, 2005 (gmt 0)

    10+ Year Member



    Cheers again; I put in the [L] because I dont have anymore rules and uploaded it, tested unc, bicc and admin, they work as should be, but now if I wanted to go to example, it doesnt redirect to clubsindex.php?folder=example

    It seems to only look at the first rule, and no matter if its true or false, doesnt go any further.

    I made a slight edit to the first rule from above, it reads:

    RewriteRule ^(admin¦intro¦photos¦news¦unc¦bicc)/ - [L]

    Just removed the clubs/ as it is in the clubs folder.

    Edit: I put the pipe characters in, but it doesnt show up on the forum.

    jdMorgan

    4:34 pm on Jun 10, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > but now if I wanted to go to example, it doesnt redirect to clubsindex.php?folder=example

    I'm sorry, this is not clear. Please provide the full requested URL-path, the expected result, and the actual result so that we can proceed.

    Just guessing, but if there's no trailing slash in the folder request, then the first rule would need to change to something like this:


    RewriteRule ^([^/]+)(/(index\.php)?)?$ /clubs/clubindex.php?folder=$1 [L]

    Put your rules in order of most-to-least-popular for best performance.

    Jim

    mealybar

    4:51 pm on Jun 10, 2005 (gmt 0)

    10+ Year Member



    The trailing slash problem is sorted in my root .htaccess, as advised in one of the earlier postings (Thanks =D).

    I want to go to: http://www.example.com/clubs/example/

    This should using the rules point to: http://www.example.com/clubs/clubsindex.php?folder=example

    Unless it is asking for any of these:
    http://www.example.com/clubs/admin/
    http://www.example.com/clubs/photos/
    http://www.example.com/clubs/intro/
    http://www.example.com/clubs/news/
    http://www.example.com/clubs/bicc/
    http://www.example.com/clubs/unc/

    This is my htaccess at the moment and it is in the clubs folder (http://www.example.com/clubs/.htaccess):

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^(admin¦intro¦photos¦news¦unc¦bicc)/ - [L]
    RewriteRule ^([^/]+)/(index\.php)?$ /clubs/clubindex.php?folder=$1 [L]
    RewriteRule ^([^/]+)/news\.php$ /clubs/clubnews.php?folder=$1 [L]
    RewriteRule ^([^/]+)/photo\.php$ /clubs/clubphoto.php?folder=$1 [L]
    RewriteRule ^([^/]+)/result\.php$ /clubs/clubresult.php?folder=$1 [L]
    RewriteRule ^([^/]+)/result([0-9]+)?\.html$ /clubs/clubresult.php?folder=$1&id=$2 [L]

    When this file is on the webserver to test it, all of the directories simply come up as normal, as if there were no htaccess file there at all, no redirects or anything.

    As you've seen in my previous posts the end 5 rules all work when on their own, I just want to exclude (admin¦intro¦photos¦news¦unc¦bicc) from being redirected to the other files.

    [edited by: jdMorgan at 5:03 pm (utc) on June 10, 2005]
    [edit reason] Examplified. [/edit]

    jdMorgan

    5:06 pm on Jun 10, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I don't see a problem in the code.

    Have you tried RewriteOptions inherit? Anything in your error log?

    [added] Oh, and be sure to flush your browser cache after making any changes to the code. [/added]

    Jim

    [edit] Corrected formatting [/edit]

    [edited by: jdMorgan at 3:05 am (utc) on June 11, 2005]

    mealybar

    5:29 pm on Jun 10, 2005 (gmt 0)

    10+ Year Member



    I all still a bit new to this, I wouldnt know how to check my error log, or what that rewrite option is?

    I'm thinking maybe it easiest to move the directories I want to be left alone, out of the clubs folder, and point to them using another rewrite rule for each?

    Thanks for your patience.

    jdMorgan

    6:19 pm on Jun 10, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > I wouldnt know how to check my error log

    It's a standard file, usually located in the same directory as your access log.

    > or what that rewrite option is?

    RewriteOptions [httpd.apache.org]

    [added] There's no obvious reason that your exclusions should not be working. This is an "easy" subject, normally. It's possible that I'm just not seeing something, since I pop in and out of here between phone calls and meetings... Or it's possible that some other code is interfering with it, or that the paths are not correct. I'm stumped.[/added]

    Jim