1

I am looking for a .htaccess that does the following:

  1. For users that visit mydomain.com, the server should return the index.html file from the root.

  2. For users that visit mydomain.com/something/, mydomain.com/anything/123/, mydomain.com/some%20encoded%20text, etc., the server should return the index.php file from the root and pass the text after www.mydomain.com/ as a PHP $_GET variable.

I tried the WordPress .htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

But this redirects everything to the index.php file.

Paul Ratazzi
  • 6,289
  • 3
  • 38
  • 50
TechAurelian
  • 5,561
  • 5
  • 50
  • 65
  • 1
    Why not just direct all traffic to index.php and if there is no additional URI passed, just serve up the index.html content? Seems odd to have a front controller for all pages in a site except for one. – Mike Brant Nov 06 '13 at 16:51
  • @MikeBrant I expect most traffic to come to mydomain.com directly, so I'm thinking serving a static html file is better than serving a php. – TechAurelian Nov 06 '13 at 16:57
  • I added the solution that was initially attempted. – TechAurelian Nov 07 '13 at 14:07

1 Answers1

1

Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^$ /index.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?f=$1 [L,QSA]

Inside index.php you can access $_GET['f'] variable that will give you requested URI.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you, works great. What PHP sanitization method would you recommend in this case for the $_GET['f'], just to prevent malicious code? (I will be using the variable as a plain string to calculate a hash for it) – TechAurelian Nov 06 '13 at 17:20
  • 1
    You're welcome. See this question for sanitization: http://stackoverflow.com/questions/205923/best-way-to-handle-security-and-avoid-xss-with-user-entered-urls – anubhava Nov 06 '13 at 17:28
  • It seems that it does not work if the encoded url contains a new line character, such as _http://mydomain.com/one%0Atwo_ I get _ERROR 404 - PAGE NOT FOUND_ – TechAurelian Nov 07 '13 at 12:24
  • 1
    To use encoded characters: `RewriteRule ^(.+)$ /index.php?f=$1 [L,QSA,B,NE]` – anubhava Nov 07 '13 at 12:26
  • Unfortunately it does not work with the new line character %0A, I still get the 404 Error. – TechAurelian Nov 07 '13 at 12:39
  • `.+` should match anything. It shouldn't cause 404 – anubhava Nov 07 '13 at 12:48
  • The problem was with the _$_ at the end. If I only use ^(.+), it works and I no longer get the error, however I no longer get the whole text in php using $_GET, only the first line. – TechAurelian Nov 07 '13 at 13:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40743/discussion-between-jamrelian-and-anubhava) – TechAurelian Nov 07 '13 at 14:07