1

This is my .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^(.*) data.php?chn=$1 [L,QSA]

I have 2 php files.

index.php and data.php

I to want call index.php when the request is for domain.com/ and data.php when the request is for domain.com/(anything) .

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Dix Pon
  • 11
  • 2
  • where do you host? do they allow .htaccess overrides? – WEBjuju Oct 27 '16 at 14:31
  • web apache2 sir – Dix Pon Oct 27 '16 at 14:41
  • for clarification, what is the name of the company that you host with? or if you are hosting yourself, can you verify that you have allow override all on http://stackoverflow.com/questions/18740419/how-to-set-allowoverride-all – WEBjuju Oct 27 '16 at 14:47
  • yes all work fine just want call index.php when query is empty (domain.com/) else call data.php (domain.com/?query) – Dix Pon Oct 27 '16 at 14:51
  • oooh. i wouldn't make htaccess do that. see solution for index.php below – WEBjuju Oct 27 '16 at 14:52

4 Answers4

0

To reroute from index.php...

if (!empty($_GET['chn'])) {
  require('data.php');
  exit();
}

data.php will already have $_GET['chn'] available to it.

or if you aren't queuing off a get var then you can check the REQUEST_URI and do the same thing:

if (@$_SERVER['REQUEST_URI']!='/')) {
  require('data.php');
  exit();
}
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
0
    RewriteEngine On
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
  RewriteRule ^/(\d+)*$ ./data.php?chn=$1

Try this.

Note: When using .htaccess in your desired case, placing / after the domain name will trigger the second RewriteRule case, meaning user will try to open literally domain.com/data.php?chn=.

Frank Helligberger
  • 46
  • 1
  • 1
  • 10
0

You need to change your regex pattern from (.*) to (.+) to avoid rewriting your homepage otherwise the request for / will also be rewritten to data.php :

Try this rule :

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/data\.php [NC]
RewriteRule ^(.+) data.php?chn=$1 [L,QSA]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

its work ! thank you all using this code :

RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ data.php?chn=$1 [L,QSA]
Dix Pon
  • 11
  • 2