Having a page within a rewritten directory?

Hello,

I am making an A to Z of the pages on my website, but i want to split it into pages so there arent too many URLs on one page.

I have created a page called “info-a-to-z.php” and rewritten it using the following:

RewriteRule ^a-to-z /info-a-to-z.php

But i want to have a file inside of that, so the pages which are split come under the same dir. Sorry if it sounds confusing, i want to do the following:

site.com/a-to-z/
site.com/a-to-z/a-to-c.php
site.com/a-to-z/d-to-f.php

and so on. How do i go about doing this? I tried searching for some help, but since its hard to explain what i wanna do its hard to find anything! :stuck_out_tongue:

Thanks

You want to basically paginate that page. It might be easier to do it in php vs rewrites.

FF,

Ryan is correct, your link generation is where you should make this “redirection.”

FWIW:

  1. For the simple redirection you used, mod_alias’s Redirect is the approriate tool, not mod_rewrite and

  2. The reason you can’t use mod_rewrite for the “generic” redirection is that you’ve not shown what you’re providing to Apache so it won’t have to guess which letter (or letters?) you’re trying to get to.

Regards,

DK

Thanks for your replies guys.

I am reluctant to use PHP because its an osCommerce page and i dont want to mess with the PHP, i dont know it, and dont wanna break it.

Also I dont want to use it only in an A to Z context, I may split up regions as dirs aswell, e.g. site.com/region site.com/region/north/ etc. I thought that a rewrite would be the best way to go.

dklynn, I read the acticle in your signature and it helps :slight_smile:

So for my A to Z i used:

rewriteRule ^a-to-z /info-a-to-z.php
rewriteRule ^a-to-z|a-to-c /info-a-to-z-2.php

which seems to work fine.

Does it really? Seems to me that code doesn’t anything, or at least shouldn’t do what you want it it do.

First of all, where are the last flags ([L] at the end of the rule)? Without those you’re burning quite a lot CPU cycles for nothing.

Second, a-to-z|a-to-c. I’d have to test to confirm, but I’m pretty sure that will match either a URL that starts with /a-to-z or or URL start with /a-to-c. However, since you also have a rule that rewrites a-to-z to /info-a-to-z.php, the first instance (rewrite /a-to-z in the second rule) will never ever happen, so drop that please.

Lastly, you don’t need to prepend / in the to part of the rule since the .htaccess is in the {DOCUMENT_ROOT} of your website. Prepending that slash will just send Apache on a wild goose chase looking for info-a-to-z.php in all the wrong places. So, drop the leading slashes there.

So, you should end up with


RewriteRule ^a-to-z info-a-to-z.php [L]
RewriteRule ^a-to-c info-a-to-z-2.php [L]

that will show /info-a-to-z.php when /a-to-z is requested, and /info-a-to-z-2.php when /a-to-c is requested.

Now, if /a-to-z and /a-to-c are the complete URLs (i.e. that’s never followed by anything, like /a-to-z/some-more-path) you should add a $ at the end

End result:


RewriteRule ^a-to-z$ info-a-to-z.php [L]
RewriteRule ^a-to-c$ info-a-to-z-2.php [L]

yeah, it does work, but I have found problems.

site.com/a-to-z -Displays info-a-to-z.php
site.com/a-to-z/a-to-c -Displays info-a-to-z-2.php

Thats is fine, however
site.com/a-to-z/ANYTHING-HERE displays info-a-to-z.php (apart from the written rules i.e. the a-to-c part.

All i want is to make my site have a better structure which seems to be hard! :frowning:

ideally:
site.com/a-to-z/
site.com/a-to-z/a-to-c.php
…and so on
site.com/regions/
site.com/regions/north.php

Second, a-to-z|a-to-c. I’d have to test to confirm, but I’m pretty sure that will match either a URL that starts with /a-to-z or or URL start with /a-to-c.

yes, you are right, i just tested that, I didnt see that one coming lol :smiley:

I thought the [L] flag was for the last rule? (i have one more) So its for the end of every rewrite rule then?

Thanks for your other tips, everyday’s a school day :wink:

site.com/a-to-z -Displays info-a-to-z.php
site.com/a-to-z/a-to-c -Displays info-a-to-z-2.php

If you want that, then your .htaccess is really wrong.
Change the second RewriteRule to


RewriteRule ^a-to-z[COLOR="Red"]/[/COLOR]a-to-c /info-a-to-z-2.php

If you want /a-to-z and a only /a-to-z to display info-a-to-z.php then you need to add an end anchor ($) to the RewriteRule, like so:

RewriteRule ^a-to-z$ info-a-to-z.php [L]

I already told you this in my previous post btw. This also holds for the a-to-z/a-to-c rule btw.

Ah yes, the infamous Last flag (I do agree, the name is a bit weird, but I wouldn’t know of a better for that odd fellah either).

The first thing you need to know is that mod_rewrite works in rounds. So, when a user requests a URL, Apache will work through all the RewriteRules to see if any of them matches. If one of them does, it will go on trying to process all of the remaining rules.
When all rules are processed, and the URL was rewritten as a result of that round, a new round will start. This will continue until the URL is not rewritten by any of the rules.
For the example of your RewriteRules, the two rules you have are mutually exlusive (or rather, the rules you should have, the rules you currently have are not). That means that if the first rule matches, the second rule will never match, and vice versa.
So, suppose a user requests the URL that is matched by the first rule. The URL will be rewritten, and Apache will try to match the second rule. But, seeing as the rules are mutually exclusive, this second rule will not match, and Apache is testing it for nothing. Of course the rule won’t match, and Apache will start the second round, which won’t do anything and we’re done.
Now, suppose you had the Last [L] flag on the first RewriteRule and again a user is requesting the URL that is matched by that first RewriteRule.
What will happen now is that the first rule matches, Apache will encounter the [L] flag and stop the current round and start a new round.
That means that it won’t check the second rule in the first round, which is nice, because we already knew that wouldn’t match.
In short, you need to add a Last flag to any rules that are mutually exclusive.

An example of where you don’t want to use the last flag:


RewriteEngine On
RewriteRule ^foo$ bar
RewriteRule ^bar$ blah

I’ll leave it up to you to decide why I wouldn’t want to use a Last flag here, and brownie points if you know when using two rules like above would be useful :slight_smile: