Forum Moderators: phranque
I'm trying to rewrite www.mydomain.com/index.php back to www.mydomain.com/, and am using the current solution:
#Rewrite index.php back to /
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule . / [R=301,L]
However if the request contains a query string, that query string is appended to the domain, for example www.mydomain.com/index.php?foo=bar rewrites to www.mydomain.com/?foo=bar
I would like to have index.php write back to / regardless of bogus query strings appended, for SEO purposes. My custom CMS produced a few links to index.php with query strings that had no effect on script execution, but were indexed in Google - I'd rather not let that happen again!
Thanks for any help. I've been reading through Apache mod_rewrite files and tutorials to no avail, but have picked up a couple of concepts that were a little enlightening.
RewriteCond %{THE_REQUEST} ^[^/]+/index\.php(\?[^\ ]*)?\ HTTP/ [NC]
RewriteRule index\.php$ [b]/?[/b] [NC,R=301,L]
The seemingly-redundant "index\.php$" RewriteRule pattern is not; It actually improves efficiency in most cases, since the RewriteCond will not be processed unless the RewriteRule pattern is matched (see docs).
The more-complex RewriteCond pattern prevents redirection of URLs containing "index.php" in the query string only, independent of any previously-executed internal redirects.
Jim
[edit] Fixed per following post to prevent future copy-and-paste errors. [/edit]
[edited by: jdMorgan at 2:25 pm (utc) on Jan. 18, 2008]
/disc/topic1/index.pl?read=01234
/disc/topic2/index.pl?read=56789
(etc.)
-- but I want only on-site referrers and/or specific hosts to access the script proper. In other words, what do you think of the following, please? Apologies if it's more mixed up than not.
Placed in /disc/.htaccess:
# If not on the server (its Host or IP):
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://98\.76\.654\.321/.*$ [NC]
# And/or hailing from a certain Host or IP:
RewriteCond %{REMOTE_HOST}!^server\.example\.com$
RewriteCond %{REMOTE_ADDR}!^987\.654\.321$
# Send all requests for ALL base index.pl files to intro:
RewriteCond %{THE_REQUEST} ^[^/disc/topic1]+/index\.pl(\?[^\ ]*)?\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} ^[^/disc/topic2]+/index\.pl(\?[^\ ]*)?\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} ^[^/disc/topic3]+/index\.pl(\?[^\ ]*)?\ HTTP/ [NC]
RewriteRule index\.pl$ http://www.example.com/disc/intro.html [NC,R=301,L]
(Note: There should be spaces before every exclamation mark.)
Right now, it says, "one or more characters not equal to a '/' or 'd' or 'i' or 's' or 'c'" etc. Square brackets define grouped *character alternatives*, and not strings...
We need to know whether it is your intent to redirect (or to not redirect) those particular "discussion topic" index.php requests.
Jim
RewriteCond %{THE_REQUEST} ^[^/]+/index\.php(\?[^\ ]*)?\ HTTP/ [NC]
-- so when I saw the "^[^/]+/index\.php" part, I thought I could specify directories by name because I have some directories with index.pl scripts that are okay to access directly, and some not.
My intent is to redirect all off-site (& not host-specified) requests to just "index.pl" ( e.g.: ^/disc/topic1/index\.pl$) in certain directories. Requests to individual posts (?read=01234) would be allowed.
Sheesh. That sounds clear as mud. Maybe this will help...
Okay ONLY if referrer is on the same server and/or MY hostname makes the request:
http://www.example.com/disc/topic1/index.pl
Okay for everyone:
http://www.example.com/disc/topic1/index.pl?read=01234
Placed in /disc/.htaccess:
...continued...
# Send all requests for ALL base index.pl files to intro:
RewriteCond %{THE_REQUEST} ^[^/]+/disc/topic1/index\.pl(\?[^\ ]*)?\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} ^[^/]+/disc/topic2/index\.pl(\?[^\ ]*)?\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} ^[^/]+/disc/topic3/index\.pl(\?[^\ ]*)?\ HTTP/ [NC]
RewriteRule index\.pl$ http://www.example.com/disc/intro.html [NC,R=301,L]
(P.S. In that last line, do we need a ^ as in: RewriteRule ^index\.pl ...?)
[edited by: Pfui at 10:18 pm (utc) on Jan. 19, 2008]
FWIW, in addition to a Comcast account, I tested using an AOL account (w/ a no-cache UA pref). I also alternated including, and commenting-out, the on-server and specific-host conditions. But no matter what I did/tried, I either redirected everyone OR I let everyone in; never just-me-to-index.pl, dangit.
If you're game to carry on, would you prefer a clean slate? :)
[edited by: jdMorgan at 9:12 pm (utc) on Jan. 21, 2008]
[edit reason] speeling. [/edit]