Forum Moderators: phranque

Message Too Old, No Replies

htaccess Rewrites Sub-Directory problem

doesn't perform properly

         

Evolve123

9:39 pm on Aug 22, 2008 (gmt 0)

10+ Year Member



Hello everyone,

I'm in a bit of a trouble here, so I'll cut right to the chase.
I currently have a couple of pages located at:

example.com/index.php?network=page1
example.com/index.php?network=page2
example.com/index.php?network=page3

etc.

I want to be able to change that to:

example.com/page1/
example.com/page2/
example.com/page3/

I have successfully done that by adding this to my htaccess:

Options +FollowSymLinks
RewriteEngine on

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

-----

Problem:

If I try to visit an actual sub-directory from the website via the main page, lets say the forums, it will will do the following: it will go to

access example.com/forums it will go to example.com/forums/?network=forums
access example.com/forums/ it will stay on the index.php page and not go to the forums

-----

How can I only make the rewrite work with my "index.php?network=" pages, and not actual sub-directories on my website?

Please help me for a solution with this.

[edited by: Evolve123 at 10:01 pm (utc) on Aug. 22, 2008]

jdMorgan

10:02 pm on Aug 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are four basic ways. First you need to understand that mod_rewrite is "URL-based" and not "filename-based." Your new "page" URLs are now "page1," "page2," and "page3" from your post above.

Your rule says, "Rewrite any URL that is in the top-level directory, including those that end with a slash, except for index.php." So, you rule will happily rewrite robots.txt, and all of your image and CSS files to the index.php file.

The four basic methods are:

  • Rewrite only a specific list of URLs to index.php -- by specifying them in the RewriteRule pattern, or by using RewriteCond patterns.

  • Rewrite *all except* a specific list of URLs, using the NOT operator "!" as above.

  • Rewrite only URLs which do not contain a files extension, by looking for the absence of a "." in the final path-part of the URL.

  • Use RewriteConds to check to see if the requested URL resolves to a 'real' file or directory, and only rewrite if it does not.

    Of these, the first three are by far the most efficient. You will have to pick one based on your current site URLs, and those you may plan to use in the future. You can mix-and-match features of all of them. It is especially important to only do the file- and directory-exists checks when absolutely necessary; Otherwise you can seriously bog down your server with all those filesystem requests.

    Jim

    [edited by: jdMorgan at 10:15 pm (utc) on Aug. 22, 2008]

  • Evolve123

    10:07 pm on Aug 22, 2008 (gmt 0)

    10+ Year Member



    Thanks for your post Jim. My question: what will be the code I'll have to add to htaccess so it will exclude every other URL that ends with a slash except for the ones it has to rewrite that are with index.php?network=page

    jdMorgan

    10:17 pm on Aug 22, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You cannot make that decision based on a file name, as described above. The method you are asking about in your question cannot work. mod_rewrite is based on the requested URL, and your requested URLs are no longer "index.php", they are "page1", "page2", and "page3"

    Jim