Redirecting index.php to /

New to mod_rewrite? This is a good place to start.

Redirecting index.php to /

Postby Timbo » Sun Aug 16, 2009 1:15 am

Hello,

I want for my website that my index.php is only available by requesting the foldername. For example i do not like the http://www.domain.com/index.php and also not the http://www.domain.com/index.php?a=b&c=d and want to create rules to forward these urls to:

http://www.domain.com/
and
http://www.domain.com/?a=b&c=d

I have been tried a couple hours now, but i cant get it working. The http://www.domain.com/ request is also being redirected, but that shouldnt happen. My current (linux) .htaccess code:

Code: Select all
# index.php cannot be opened [L,R=301]
RewriteBase /
# ROOT folder
# without url parameters - working
RewriteCond %{QUERY_STRING} ^$
# some tryout with only the .php extension because i cant get it working
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^index\.(php|phps|html|htm|xml)$ http://www.google.nl/?q=index-without-parameters

# with url parameters
RewriteCond %{QUERY_STRING} !^$
# some tryout with only the .php extension because i cant get it working
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^index\.(php|phps|html|htm|xml)(.*)$ http://www.google.nl/?q=index-with-parameters


Does anyone have the code for this?
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby richardk » Sun Aug 16, 2009 7:23 am

richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Timbo » Sun Aug 16, 2009 11:19 pm

Thanks! That one fixed the problem. I had to edit it a bit for accepting my other file extensions but i learned from that what the question mark also for functionality provides. hehe :)
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby Timbo » Mon Aug 31, 2009 9:44 am

Richard,

I want to expand my current code:

Code: Select all
RewriteBase /

# no match with the used keyword subdomains
RewriteCond %{HTTP_HOST} !^(2009|2010)[\.]{1}mydomain\.com$

# its surely not the destination domain ( endless browsers location loopings )
RewriteCond %{HTTP_HOST} !2009.mydomain\.com$

# some tryouts to let the index leave there, but dont work:
# RewriteCond %{REQUEST_URI} !^/$
# RewriteCond %{REQUEST_URI} !^$

# rewrite with 301
RewriteRule ^(.*)$ http://2009.mydomain.com/$1 [L,R=301]


This code does permanently move all contents of my whole website to the '2009' subdomain. Thats good, but i want the website index remains as it is. So 'www.mydomain.com/(index.*)' must stay to exist, and must not become '2009.mydomain.com/(index.*)'. Is that possible with htaccess?
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby richardk » Mon Aug 31, 2009 10:47 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.[^/]+$
RewriteRule ^(.+)$ http://2009.example.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Timbo » Tue Sep 01, 2009 1:56 am

richardk wrote:Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.[^/]+$
RewriteRule ^(.+)$ http://2009.example.com/$1 [R=301,L]


Hi, but what if the requested page is:
http://www.mydomain.com/


Then it doesnt match with 'index.*'.. Is that also possible to match that situation?
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby richardk » Tue Sep 01, 2009 9:45 am

Right, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} !^(www\.)?example\.com/index\.[^/]+$
RewriteRule ^(.+)$ http://2009.example.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Timbo » Tue Sep 01, 2009 12:06 pm

richardk wrote:Right, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} !^(www\.)?example\.com/index\.[^/]+$
RewriteRule ^(.+)$ http://2009.example.com/$1 [R=301,L]


Hm it doesnt work. Isnt this impossible because we have to check something which needs to be empty (request_uri)?
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby richardk » Tue Sep 01, 2009 2:04 pm

I don't think it's impossible, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# It can't be 2009.example.com or 2010.example.com.
RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
# It can't be www.example.com, example.com,
# www.example.com/index.* or example.com/index.*
RewriteCond %{HTTP_HOST}%{REQUEST_URI} !^(www\.)?example\.com/(index\.[^/]+)?$ [NC]
RewriteRule ^(.*)$ http://2009.example.com/$1 [R=301,L]


Do you have other rules in this .htaccess file? If you do post the whole .htaccess file/mod_rewrite.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Timbo » Tue Sep 01, 2009 2:38 pm

richardk wrote:I don't think it's impossible, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# It can't be 2009.example.com or 2010.example.com.
RewriteCond %{HTTP_HOST} !^(2009|2010)\.example\.com$ [NC]
# It can't be www.example.com, example.com,
# www.example.com/index.* or example.com/index.*
RewriteCond %{HTTP_HOST}%{REQUEST_URI} !^(www\.)?example\.com/(index\.[^/]+)?$ [NC]
RewriteRule ^(.*)$ http://2009.example.com/$1 [R=301,L]


Do you have other rules in this .htaccess file? If you do post the whole .htaccess file/mod_rewrite.


This works! The rest of the file is empty.
Thank you! :)
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 33 guests

cron