2012-08-31 27 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

相關問題