.htaccess匹配任何内容并发送到PHP文件

时间:2012-08-31 13:31:09

标签: php .htaccess

我的正则表达式尝试匹配所有结果并重定向到页面。我想将请求的地址发送到页面:

RewriteRule ^/[\w\W]*$ processor.php [R,NC,L]

例如,我的地址是:

www.mywebsite.com/mySecretCode123

我希望我的php文件能够读取它:

<?php echo $mySecretCode123; /* outputs 'mySecretCode123' */ ?>

我该怎么做?

2 个答案:

答案 0 :(得分:3)

.htaccees

# RewriteCond is condition - make rewrite only if file doesn't exists
# (.+) means "any character, one or more"
RewriteCond  %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ processor.php?mySecretCode=$1 

PHP

<?php echo $_GET['mySecretCode']; ?>

答案 1 :(得分:2)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ processor.php?$1=$1 [L,QSA]