Target and rewrite one url ignoring any others, with the issue being that the others partially match it

StackOverflow https://stackoverflow.com/questions/19558345

Question

Ive exhausted my patient first, my energy then, and finally my sanity, trying to make a rewrite redirect for a homepage of the kind:

http://www.mydomain.com/index.php

to

http://www.mydomain.com/

Ive indeed done that successfully with something like:

RewriteRule ^/index\.php$ /$ [R=301,L]

However the issue is that the rest of urls are done like this

http://www.mydomain.com/index.php?page=this-is-a-page-title

Which makes the previous rewrite to break them turning them into:

http://www.mydomain.com/?page=this-is-a-page-title

Ive have not managed to properly write a rewritecond to exclude all urls containing the string

index.php?page=

Or preferably something to directly target the homepage only, this lead me to look for something to be put after the url like

http://www.mydomain.com/index.php(here)

that would tell mod_rewrite to not go for any url longer than that, but i could not understand how to do it either with a rewriterule only or with a rewritecond of the request_uri ! kind. And in fact found a post here where ppl stated that regex is to match something, and is not efficient at all to try match nothing. Ive tried near a hundred, remaking them with my best knowledge, and got most not working, and some directly breaking url, and even 500 errors.

You decide whatever you feel is the more efficient solution.

Ive learnt more than the almost nothing i knew about regex and mod_rewrite, but i think im past beyond the point were i should ask for some help. Ive tried for two days, read a lot, here and there, apache docs, and i have the feeling ive past almost touching the working line, probably with some mistake in punctuation. Im also interested in understanding whatever is shown here to fix what im sure is a misconception on my part either about regex or about how to properly lay in a correct order a rewrite or rewritecond like this.

Was it helpful?

Solution

You could use RewriteCond to condition the following rewrite rule. Something like this:

RewriteCond %{QUERY_STRING} =""
RewriteRule ^/index\.php$ / [R=301,L]

The condition here is that the query string is empty, which seems to be what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top