Wildcard in URL (mod_alias or mod_rewrite)
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.
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]
Place in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
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).This will work for URLs like
/whatever
, but will do nothing for/what-ever
,/hello/pinky
,/hello123
.
精彩评论