2

I need to have URLs such as mydomain.com/whatever, where "whatever" can be any arbitrary string, all call the same php file where it sorts out what to display (or displays a 404). However, I want files and other php files to work normally (anything that is otherwise aliased, or that actually exists in the file system).

A simple AliasMatch /* myphpfile.php (after all the other Aliases in httpd.conf) works fine on my own setup, but on a production server, the wildcard alias sends all the other php files to myphpfile.php. I'm not sure what else might be confusing things.

Technically the whatever string will be alphabetic and lower case, so it can filter for that, but all attempts I've made with regex's haven't been successful.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
rob
  • 9,933
  • 7
  • 42
  • 73

1 Answers1

2

Use these rules (you need mod_rewrite):

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteRule ([a-z]+) /myfile.php [L]
  1. Place in .htaccess in website root folder. If placed elsewhere some tweaking may be required.

  2. This will rewrite (internal redirect) all NON-EXISTING single-lowercase-word requests to /myfile.php, where using $_SERVER['REQUEST_URI'] script can determine which URL was called and decide what to do (routing).

  3. This will work for URLs like /whatever, but will do nothing for /what-ever, /hello/pinky, /hello123.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Thanks, although it looks like the production server doesn't have mod_rewrite so if there is any way to do it with mod_alias alone, that would be awesome. – rob Jul 30 '11 at 04:50
  • @rob Unfortunately I do not know how it can be done with mod_alias. I recommend contacting your hosting company (or wherever your production server is) to enable it (mod_rewrite is so widely used, then I see no benefits having this disabled -- unless it is very cheap hosting where they have hundreds of sites on a single server). – LazyOne Jul 30 '11 at 08:33
  • It's a client's server, which they have pretty locked down. If I can do things without bugging the IT guy (and waiting for him to get around to it), it's always better. :) But thanks, I'll go ahead and tell them they need this, and use your solution. – rob Jul 30 '11 at 18:57