homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess Deep Linking

 10:14 am on Oct 24, 2011 (gmt 0)

Hi all, I'm trying to ensure a single directory can rewrite multiple parameters and create a number of virtual folders deep.

For this example, I am just trialling two levels deep.

I have tried:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?var1=$1&var2=$2 [NC]
RewriteRule ^([^/]+)/ index.php?var1=$1 [NC]

Each rule individually works fine, and the page renders, but when I try and GET the var1 parameter using the 2 level deep folder structure it prints index.php not the actual folder name. i.e. abc

Printed: index.php from GET['var1']
URL: http://www.example.com/abc/xyz/
Should have printed: abc

Thanks in advance.



 5:47 pm on Oct 24, 2011 (gmt 0)

You need the [L] flag on every rule and the $ symbol on each RegEx pattern.

Your extensionless URLs should not end with a trailing slash. Trailing slash is reserved as the URL for a folder or for the index page in a folder.

RewriteRule does not "make" new URLs. It looks at the request made after the link is clicked. Therefore, the first step is to change the URLs in the links on your pages to point to the friendly URLs.


 9:57 pm on Oct 24, 2011 (gmt 0)

Hi, so I edited the rules, and the first rule on it's own lets me get the two folder variables correctly using:

RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L]
...gets abc, xyz for the two folders

...however the

rule that handles only 1 folder gets index.php as the first variables instead of abc on it's own, and when added to the .htaccess file as a second rule, makes the 2 folders rule retrieve index.php for the first variable also

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

..my .htaccess file as it stands is:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L]
RewriteRule ^([^/]+)$ index.php?var1=$1 [L]

Do I need an OR condition?


 11:05 pm on Oct 24, 2011 (gmt 0)

Have you got any other RewriteRules or any redirects anywhere in this site?


 1:51 am on Oct 25, 2011 (gmt 0)

At the very top level of the site. This rule addresses the 301 redirect.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Interesting my rule is for the first level folder and everything down:

i.e. http:www.test.com/folder/

the .htaccesss exists in here, and should only work for this folder.

It's weird because when I test with the non www version it puts $url as the second folder.

Any ideas?


 3:44 am on Oct 25, 2011 (gmt 0)

Y'know, I kinda suspect this is a php problem rather than an htaccess one. You might try first creating some query strings with literal text, like "foo&bar&widget", and feed it into your php code to see if it's returning what it's supposed to. Otherwise you're potentially trying to juggle two problems at once. PHP is less dangerous than htaccess, so get that fine-tuned first.


 6:58 am on Oct 25, 2011 (gmt 0)

Your non-www to www rule is missing escaping of literal periods. It also doesn't redirect www requests with appended port number. See recent threads for better code. This particular sub-topic comes up almost every day.

I would have all the rules in one .htaccess file in the root so that you can control the order they are parsed. Make sure you are not using Redirect or RedirectMatch anywhere within the site as that will likely have unintended consequences.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved