Forum Moderators: phranque

Message Too Old, No Replies

Virtual directories: how can I fix my regexp?

regexp

         

mattpointblank

7:32 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Hi everyone.

I want to create the illusion of the following url:

mysite.com/ls/section/category/articlename

which really maps to:

mysite.com/ls/article.php?secID=section&catID=category&artID=articlename

Here's what I tried:

RewriteEngine On
RewriteRule ^ls/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ article.php?secID=$1&catID=$2&artID=$3 [L]

This doesn't work - the page gives a "Server error" message, as does /ls/index.php :(

Any tips? The regexp is supposed to check for any string since the section names can be alphanumeric and contain basic symbols (hyphens etc). Thanks!

mattpointblank

7:43 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Oh, and if it helps, Apache is giving this error:

File does not exist: C:/xampp/htdocs/ls/ls1

(ls1 is $section)

jdMorgan

7:59 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace "([A-Za-z0-9]+)" with "([^/]+)" -- i.e. "match characters until you find a slash"
Do not use HTML character-entity encoding in mod_rewrite: Replace all occurrences of & in your code with the single character "&".

Completely flush your browser cache before testing after any code changes.

If it still doesn't work, post the relevant contents of your server error log.
And please post the exact URL(s) you are using to test this code.

Jim

g1smd

9:02 pm on Sep 30, 2008 (gmt 0)

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



Once you have got this bit working, don't forget the other steps you'll need...

Externally redirect requests for the dynamic URLs to the folder-based URL format, and force www into the URL at the same time for those, so that you can't serve duplicate content.

You might also want to add another redirect to strip index file filenames from URLs, forcing www at the same time for those.

Finally, you'll need one last redirect to force www if any non-www URL not covered by the other redirects is requested.

Before that, let's get the rewrite working first.

[edited by: g1smd at 9:06 pm (utc) on Sep. 30, 2008]

mattpointblank

9:02 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



New code is:

RewriteEngine On
RewriteRule ^ls/([^/]+)([^/]+)([^/]+) article.php?secID=$1&catID=$2&artID=$3

Apache Error log (the above was also from this):
"File does not exist: C:/xampp/htdocs/ls/ls1"

The URLs I'm using:

[127.0.0.1...]

When I run the following, it works:

[127.0.0.1...]

g1smd

9:09 pm on Sep 30, 2008 (gmt 0)

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



^ls/([^/]+)([^/]+)([^/]+)

should be:

^ls/([^/]+)[b]/[/b]([^/]+)[b]/[/b]([^/]+)$

You may or may not need a trailing / on the end of that pattern depending on which URL format you actually use.

Don't set it up so that both work, as that is a Duplicate Content scenario.

mattpointblank

9:25 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Still getting an error 404 (same apache error as above) - I'm doublechecking that the rewrite rule stuff is enabled, AFAIK it is.

This is my code, again:

RewriteEngine On
RewriteRule ^ls/([^/]+)/([^/]+)/([^/]+)$ article.php?secID=$1&catID=$2&artID=$3

g1smd

9:28 pm on Sep 30, 2008 (gmt 0)

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



article.php?secID=$1&catID=$2&artID=$3

Should there be a /ls/ on the front of that part too?

If the rule is in the.htaccess in the /ls/ folder, then you don't need the /ls/ to be specified on any part of the rule (left or right).

[edited by: g1smd at 9:30 pm (utc) on Sep. 30, 2008]

mattpointblank

9:30 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Yes, sorry - but still gives "File does not exist: C:/xampp/htdocs/ls/ls1". Is it likely to be a config option on my local test server? I could try this on a remote site that definitely works with rewrite rules.

mattpointblank

9:34 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Thanks g1smd, that latter point was the problem. Thank you so much for the help!

g1smd

9:59 pm on Sep 30, 2008 (gmt 0)

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



RewriteRule cannot see "higher" folders.

They are stripped off (as is the final leading / of the URL when used in .htaccess).

g1smd

10:28 pm on Sep 30, 2008 (gmt 0)

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



Now you need to get all the redirects working, as detailed above.

They stop any *direct* access to the content via other URLs.