Forum Moderators: phranque

Message Too Old, No Replies

Rewrite (regexp) problem when I want to replace 'index.php?/' to '/'

apache rewrite regexp replace/match problem

         

TB_Web

11:00 pm on Oct 1, 2011 (gmt 0)

10+ Year Member



Hi!

I had problems with sharing links to facebook (using sharer.php) because the urls what I want to share contains a question mark (? sign).
I didn't find any workaround about it, so I eliminated the ? from the url with rewrite.

But now I have a new problem about it, google indexed
'http://termeszetbolond.hu/index.php?/hun/Magyarorszag-allatvilaga'
but the new working url is
'http://termeszetbolond.hu/hun/Magyarorszag-allatvilaga'

I figured out a buggy rewriterule:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?(/[^\ ]*)?\ HTTP/
RewriteRule ^index\.php\?(/(.*))?$ http://termeszetbolond.hu/$2 [R=301,L]


It's redirects from 'http://termeszetbolond.hu/index.php?/hun/Magyarorszag-allatvilaga' to 'http://termeszetbolond.hu/?/hun/Magyarorszag-allatvilaga'

Please tell me how can I include the '?' after 'index.php' into the regexp.

g1smd

11:46 pm on Oct 1, 2011 (gmt 0)

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



RewriteRule
can see only the path part of the HTTP request, not the query string.

Delete
\?(/(.*))?$
from the
RewriteRule
because it will NEVER match.

In the
RewriteCond
the
\?(/[^\ ]*)?\ 
part matches the literal question mark and appended query string data.

This captured data is available to use in the RewriteRule target as
%1
. That's
%1
not
$2
.

Something in there will need slight adjustment in order to avoid having a double slash in the target URL.

I'd suggest using
\?/([^\ ]*)?\ 
or similar, in place of the existing
\?(/[^\ ]*)?\
code.

TB_Web

12:17 am on Oct 2, 2011 (gmt 0)

10+ Year Member



Thank you!
It's more better than my buggy version, but now repeats the second part of the url
'http://termeszetbolond.hu/index.php?/hun/Magyarorszag-allatvilaga' -> 'http://termeszetbolond.hu/hun/Magyarorszag-allatvilaga?/hun/Magyarorszag-allatvilaga'

The current rewrite code is:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?/([^\ ]*)?\ HTTP/
RewriteRule ^index\.php http://termeszetbolond.hu/%1 [R=301,L]

g1smd

12:47 am on Oct 2, 2011 (gmt 0)

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



Add a question mark after the
%1
part, something like
%1? [R=301,L]
to suppress the original query string being added back on.

Forgetting to add that question mark is my most common typo.

Do you want this only for the index.php in the root, or do you also need it for folders?

TB_Web

12:52 am on Oct 2, 2011 (gmt 0)

10+ Year Member



Thank you very much!

It's works! :)

The final code is:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?/([^\ ]*)?\ HTTP/
RewriteRule ^index\.php http://termeszetbolond.hu/%1? [R=301,L]

lucy24

1:26 am on Oct 2, 2011 (gmt 0)

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



[oops-- minor glitch here]

TB_Web

2:12 am on Oct 2, 2011 (gmt 0)

10+ Year Member



Only in the root folder, so you solved this problem.
Thank you again!