Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Htaccess problem


g1smd - 2:28 pm on Nov 29, 2012 (gmt 0)


Why is (php) in brackets? Not needed.

Why are slashes escaped and in character groups?
[\/]? simplifies to /?

^.*\. in your pattern should be replaced by ^([^/]+/)+\. or similar. Never use .* at the beginning or in the middle of a pattern.

Your "four optional somethings" makes the pattern very ambiguous.

You should either have four separate rules, with 2, 3, 4 and 5 groups, OR use
^([^/]+)/(([^/]+/)*)([^/.]+)$ (use $1, $2, $4) or ^([^/]+)/(([^/]+/)*[^/.]+)$ (use $1, $2) to capture multiple folder groups all at once in $2 (and $4 in first code snippet) and then explode and count them within the PHP script. Map $1 to the internal php file.

Every rule needs the
[L] flag.

Allowing optional slash on the end of the URL means that two different URLs can deliver the same content. You should pick one (without slash) to deliver content, and redirect requests for the other (with slash) using a separate preceding rule.

I am not exactly sure what
RewriteCond %{REQUEST_FILENAME} ^.*\.(php)$
is for. I expect you meant:
RewriteCond $1\.php -f
to test that the first part of the requested URL matches to a real php file.
To map the incoming URL request to a real physical PHP file, look at the rule target and replace
$1?q1=... with /$1.php?q1=...

Putting it all together gives:
RewriteCond %{REQUEST_URI} !-d
RewriteCond $1\.php -f
RewriteRule ^([^/]+)/(([^/]+/)*[^/.]+)$ /$1.php?multi=$2 [QSA,L]

or you could use four separate rules, with 2, 3, 4, and 5 groupings.

You'll also need a rule redirecting requests "with slash" to "without slash" as well as the standard non-www/www canonicalisation redirect before any of the rewrites.

Use example.com in all code examples in this forum. It stops the auto-linking function from activating.


Thread source:: http://www.webmasterworld.com/apache/4523539.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com