【发布时间】:2014-01-04 05:24:52
【问题描述】:
我想要以下:
myhomepage.com myhomepage.com/index.php
myhomepage.com/mypage myhomepage.com/index.php?page=mypage
myhomepage.com/mypage/mymethod myhomepage.com/index.php?page=mypage&method=mymethod
myhomepage.com/api/logout myhomepage/api/logout.php
这是我的 .htaccess 文件:
RewriteEngine On
RewriteBase /
RewriteRule ^/api/(.*)$ api/$1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ index.php?page=$1&method=$2&item=$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?page=$1&method=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1
一切正常,但api 的规则似乎不起作用。
更新 1:
RewriteRule ^/api/(.*)$ api/$1.php
当我调用 myhomepage.com/api/logout 时,它会将其重写为 myhomepage.com/index.php?page=api&method=logout
更新 2:
我的第一个想法是,也许有类似默认 rewrite_module 的东西。它将所有不是文件的东西重写为 index.html 或类似的东西。特别是当我使用 XAMPP 时。
更新 3:
经过一点试验,我发现,我不能用api 调用它,因为它是一个目录。例如,当我用myhomepage.com/ap/logout 和RewriteRule ^ap/(.*)$ api/$1.php 重写它时,它工作正常。任何想法如何使用目录名称进行这项工作?
更新 4:
我将链接 todo.js(从 Windows/System32/drivers/etc 中的主机)重定向到 127.0.0.1
然后我编辑了httpd-vhosts.conf 并添加了以下内容:
<VirtualHost todo.js:80>
ServerAdmin slajaa09@htlkaindorf.at
DocumentRoot "C:/xampp/htdocs/todo"
ServerName todo.js
ServerAlias todo.js
</VirtualHost>
而我的api 文件夹是htdocs/todo 的子目录
*解决方案:*
这是我的问题的最终解决方案:
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule api/([^/]+)/?$ api/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.*)$ index.php?page=$1&method=$2&item=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.*)$ index.php?page=$1&method=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
特别感谢 anubhava!
【问题讨论】:
标签: php regex apache mod-rewrite url-rewriting