2012-08-31 26 views
3

我的正则表达式尝试匹配所有结果并重定向到页面。我想发送请求的页面地址:.htaccess匹配任何内容并发送到PHP文件

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

例如,我的地址是:

www.mywebsite.com/mySecretCode123 

我想我的PHP文件,以便能够阅读:

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

我该怎么做?

回答

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']; ?> 
+0

我使用' RewriteRule ^/[\ w \ W] * $ processor.php?key = $ 1 [R,NC,L]'而不是'(。*)',因为我认为它可能会更快。这是真的吗,你觉得呢? – user1477388

+1

@ user1477388我不确定,但我认为它相同或更快。但你应该多关心php,db速度要比mod_rewrite速度快。 – Peter

+0

感谢您的帮助:) – user1477388

2

启用mod_rewrite的,并通过httpd.conf的.htaccess,然后把这个代码在你.htaccessDOCUMENT_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] 
+0

感谢您的详细解答。 – user1477388

相关问题