簡體   English   中英

htaccess重寫規則重定向到文件與否

[英]htaccess rewrite rule to redirect to file or not

我一直在嘗試編寫一組htaccess規則,它們將執行以下操作:

  • 使用網址:mydomain.com/blog/something

    • 如果存在名為blog.php的文件
      web目錄(index.php中的目錄)然后重定向到
      blog.php的?URL =東西

    • 如果blog.php不存在,請重定向到index.php?url = blog / something

    • 編輯:mydomain.com/blog/something是一個例子,它可以是任何url字符串,htaccess應該在web目錄中搜索對應於url字符串的第一部分的文件,在這種情況下是“blog”

在這兩種情況下,我都可以使用$ _GET ['url']從url獲取參數,以便在我的php腳本中啟動任何操作。

我目前有:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]

</IfModule>

但不幸的是,這不起作用,我不知道為什么,我對PHP很熟練,但我的htaccess技能非常有限。 我不知道[L]選項是否應該使用,或者我是否應該使用更多選項。 目前無論url字符串是什么,$ _GET ['url']都會返回“index.php”。

如果需要更多信息,請詢問。

我希望有人可以幫助問候盧克

我希望以下指令對您有用。

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Apache mod_rewrite參考: http//httpd.apache.org/docs/2.2/mod/mod_rewrite.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM