Forum Moderators: phranque

Message Too Old, No Replies

Rewriterules for different folder levels

         

chrisguk

12:35 pm on Apr 18, 2012 (gmt 0)

10+ Year Member



Hi,

After some great assistance from g1smd and Lucy (from apache web server forum) I have got to a stage where im a little stuck. Firstly in plain English I will try to explain the result im trying to achieve:

NB. Please feel free to advise if my structure is incorrect.

A user is sat on my homepage and they see in the menu bar "About us", in the status bar the URL is displayed as follows:

[url]http://www.example.com/about[/url]

The content file for the page about is called about.php and is located in the webroot above public_html (I have this part working with the following rule)


RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?page=$1 [L]


While navigating the "About us" page the user sees a link to view someones profile called "Profiles", as they hover over that link in the status bar the URL is displayed as follows because I wanted profiles to be a child of about:

[url]http://www.example.com/about/profiles[/url]

Presently I have been unable to achieve this because of mainly a lack of knowledge when it comes to regex etc.

In terms of my folder structure on the server what would you recomend? is it necessary to place all of the files that are "Child files" to the about.php page in a folder called about, or, does the name of the folder not matter?

I am aware that if you have a folder with the same name as the parent file = "about.php" then problems occur, or at least they do for me.

The closest I got was using the following rule but as g1smd quite rightly advised I shouldnt use the optional / otherwise it can lead to duplicate content:

Profiles.php is now located in a folder called "us"

About.php is in the webroot

RewriteRule ^us/?(([^/]+/)*[^/.]+)?$ /us/index.php?page=$1 [L]


I believe this approach is completely wrong about as the URL would be displayed as:

[url]http://www.example.com/us/profiles[/url]

Instead of what I want to achieve:

[url]http://www.example.com/about/profiles[/url] (And all the other files located in the same folder like below)
[url]http://www.example.com/about/anotherprofiles[/url]
[url]http://www.example.com/about/biggerprofiles[/url]

For those kind people that can help me here, would you be kind enough to give advice on the structure of my link too.

Many thanks in advance :)

lucy24

9:45 pm on Apr 18, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Before anything else: Make sure your site has no relative links anywhere. That is, links in the form blahblah/otherstuff.html or images/pic3.jpg, let alone ../backhere/otherplace.html. When you click on a link, your browser itself is issuing a request. So it goes by where the browser "thinks" you are, not by where you "really" are.

Next: Make all your internal links show what you want the user's address bar to show, not the "real" location of the material. (I believe g1 has a macro that says this.) If you want the user to see /about/ then make your links say /about/.

As with most* Rewrites, there will be two rules. The first one-- which is just for insurance-- is the one that goes (in its potentially simplest form)

RewriteCond %{THE_REQUEST} \?
RewriteRule uglyURL http://www.example.com/prettyURL [R=301,L]

The second of the pair is the one with a Rewrite alone, from "URL you want people to see" to "URL where the content really lives". For example:

RewriteRule ^about/{stuffyouwanttodump}/(stuffyouwanttokeep) /us/index.php?profile=$3

Details of course will vary. But once you've got the framework, you just plug in the exact words as needed. You may even be able to collapse different kinds of request into a single Rule.


* I started out saying "all Rewrites". But if you're rewriting a hotlinker or a questionable browser, you really don't care what they asked for in the first place. So far, the googlebot doesn't seem to go around spoofing MSIE 4 in order to zap you for Duplicate Content ;)

chrisguk

8:03 pm on Apr 20, 2012 (gmt 0)

10+ Year Member



Lucy hi,

Not to sound like a retard im not quite familiar with regex tech talk at the moment.

A quick example for you to hopefully translate for me.

The URL I want the user to see:

http://www.example.com/about and
http://www.example.com/about/profiles

The physical location of the files:

about.php is in a folder called content
profiles.php is in the folder called content/about

I use the following PHP code to request the pages:

$page = isset($_GET['page']) ? $_GET['page'] : 'home'; 

include($_SERVER['DOCUMENT_ROOT']. '/include/header.php');

include('content/'.$page.'.php');

include($_SERVER['DOCUMENT_ROOT']. "/include/footer.php");

lucy24

1:16 am on Apr 21, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So if you had no rewriting or redirecting, then

www.example.com/content/about.php

would take people to your "about" page, and

www.example.com/content/about/profiles.php

would take them to "profiles"?

Ugh. I kinda don't like the way you have a file and a directory with the same name in the same location. (Through some ghastly oversight, I've got the same setup myself in one directory. But I'm not going to change it, because the search engines would then be eating 301s forever.)

Is there more than one possible "about" page and/or more than one possible "profiles" page? I can't remember if the previous thread talked about query strings. Or is there just one of each? If there's only one of each page, it's your basic pattern:

RewriteCond %{THE_REQUEST} content/
RewriteRule content/about\.php http://www.example.com/about [R=301,L]

paired with

RewriteRule ^about$ http://www.example.com/content/about.php [L]

and similar pair for content/about/profiles.php with about/profiles.

If there's a query string, add complications ;)

Incidentally, you're free to call it simply /profiles without preliminary /about. Once you're rewriting, you could call the two files /foobar and /gizmo for all mod_rewrite cares. But it's definitely safer not to use names that happen to be the names of real directories. You don't want mod_dir sticking its nose in where it isn't wanted.

chrisguk

1:28 am on Apr 21, 2012 (gmt 0)

10+ Year Member



Hi,

Yes there is only one profiles.php and only one about .php.

In fairness because my project is at the beginning they dont have to be in any of those folders.

What do you suggest?

lucy24

4:59 am on Apr 21, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd use those names. So the user sees

www.example.com/about
www.example.com/profiles

and then you go fetch the content from wherever it really lives. Just make sure you never create real top-level directories called /about/ or /profiles/ or there may be Unintended Consequences.