Welcome to WebmasterWorld Guest from 54.227.5.198

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess Deep Linking

     

username

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

5+ Year Member



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.

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

username

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

5+ Year Member



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?

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

username

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

5+ Year Member



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?

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month