Forum Moderators: phranque

Message Too Old, No Replies

Redirect subdirectory to root index.php with parameters

         

lat9

3:07 pm on Sep 14, 2012 (gmt 0)

10+ Year Member



I'm trying to redirect all requests of the form

www.example.com/folder/<followed by anything>

to

www.example.com/index.php?page=main<followed by anything>

The code for www.example.com is in a subdirectory itself. Here's what I've currently got in my .htaccess
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#
RewriteRule ^folder/?(.*)$ index.php?page=main&$1 [R=301,L]


The original request looks like

www.example.com/folder/?p=5

and it gets redirected to

www.example.com/index.php?page=main/

but what I need/want is

www.example.com/index.php?page=main&p=5

lat9

3:27 pm on Sep 14, 2012 (gmt 0)

10+ Year Member



Isn't it always the case ... right after I posted this, I got closer:

RewriteRule ^folder/(.*)$ index.php?page=main&$1 [QSA,R=301,L]

This results in

www.example.com/folder/?p=5 being properly rewritten to www.example.com/index.php?page=main&p=5/

However, www.example.com/folder/?p=5#xyz gets rewritten to www.example.com/index.php?page=main&p=5/#xyz ... so I'm still doing something wrong. I also don't get where the backslash after the p=5 is coming from.

Any guidance would be much appreciated.

phranque

4:13 pm on Sep 14, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the #xyz in the url is a fragment identifier and is held by the browser until the request is resolved, so that's not being sent with the request nor is it appended by the RewriteRule.

if you use a header checker such as the Live HTTP Headers add-on for firefox you can see this clearly.

i'm also not sure where that slash is coming from.
are you sure there isn't some trailing slash canonicalization RewritRule directives in your .htaccess or the server config file?


i would ditch the index.php.
set your default directory index document to index.php and then you don't need it in the url.

add some mod_rewrite directives to remove the default directory index document from any such requested urls.

the redirect should target a fully qualified url so that regardless of the requested hostname it gets redirected to the canonical hostname.

add some general mod_rewrite directives to redirect non-canonical hostname requests when everything else is ok.

lat9

5:23 pm on Sep 14, 2012 (gmt 0)

10+ Year Member



phranque, thanks for the reply; I learn something new each time I post here!

You're right, the problem is that there's some code somewhere in my WordPress installation (I'm in the process of *trying* to imbed WordPress into a Zen Cart) that insists on adding that *^*& trailing slash! I'll track it down somehow.

lucy24

1:13 am on Sep 15, 2012 (gmt 0)

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



www.example.com/folder/?p=5 being properly rewritten to www.example.com/index.php?page=main&p=5/

No, it isn't. It's being properly REDIRECTED.

If a redirect is really what you want, then include the full protocol-plus-domain in the target:

RewriteRule ^folder/(.*)$ http://www.example.com/index.php?page=main&$1 [QSA,R=301,L]

Otherwise everyone stays in whatever form of the domain name they originally requested: with or without www., with or without port number, possibly even IP address instead of domain name.

The RewriteBase directive is almost never needed, and definitely never
RewriteBase /
since that is the default anyway.

But do you really want a redirect? It seems as if you are sending people to an uglier URL than the one they started out at, so you'd want a rewrite instead. That means no protocol-plus-domain, and no [R] flag.

You need to find out where and when the ending / is added so you don't come out with multiple redirects. If you can't stop the rule from executing before your Rewrite, change the .* to
([^/]*)/?
so the / isn't included in the capture. You'll need further tweaks if the request can ever involve a nested page like
directory/subdirectory/subsubdir/{et cetera}

g1smd

6:46 am on Sep 15, 2012 (gmt 0)

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



Never redirect to a URL that includes the index filename. The root URL of a site (or of any folder) ends with a trailing slash.

Make sure you know the difference between an external (301, 302, 307) redirect and an internal rewrite. The RewriteRule syntax for each is only slightly different.