URL 使用 htaccess 进行路由
URL routing with htaccess
我目前正在学习 PHP 并且我一直在尝试制作一个 PHP MVC 网站。但是我无法让 URL 路由在我的配置文件中正常工作。
这是我的 bitnami httpd-vhosts.conf 文件(因为我正在 bitnami 服务器上开发它):
<VirtualHost *:443>
ServerName dev.example.com
DocumentRoot "/opt/bitnami/apps/dev/htdocs"
SSLEngine on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule . /index.php?url=%{REQUEST_URI} [L,QSA]
SSLCertificateFile "/opt/bitnami/apps/dev/conf/server.crt"
SSLCertificateKeyFile "/opt/bitnami/apps/dev/conf/server.key"
Include "/opt/bitnami/apps/dev/conf/httpd-app.conf"
</VirtualHost>
根据我的理解,这就是我所期望的。直接这个 URL:
https://dev.example.com/contact
至:
https://dev.example.com/index.php?url=contact
然而我总是需要这个URL
https://dev.example.com/index.php?url=/contact
任何帮助将不胜感激,因为我已经为这个问题苦苦挣扎了几个小时。
这里是 Bitnami 工程师。
如果要去掉URL的/,只需要在@Karkouch的解决方案中做一个小改动。使用此配置行
RewriteRule "^/?(.*)" /index.php?url= [NC,L,QSA]
然后重启 Apache :)
我目前正在学习 PHP 并且我一直在尝试制作一个 PHP MVC 网站。但是我无法让 URL 路由在我的配置文件中正常工作。
这是我的 bitnami httpd-vhosts.conf 文件(因为我正在 bitnami 服务器上开发它):
<VirtualHost *:443>
ServerName dev.example.com
DocumentRoot "/opt/bitnami/apps/dev/htdocs"
SSLEngine on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule . /index.php?url=%{REQUEST_URI} [L,QSA]
SSLCertificateFile "/opt/bitnami/apps/dev/conf/server.crt"
SSLCertificateKeyFile "/opt/bitnami/apps/dev/conf/server.key"
Include "/opt/bitnami/apps/dev/conf/httpd-app.conf"
</VirtualHost>
根据我的理解,这就是我所期望的。直接这个 URL:
https://dev.example.com/contact
至:
https://dev.example.com/index.php?url=contact
然而我总是需要这个URL
https://dev.example.com/index.php?url=/contact
任何帮助将不胜感激,因为我已经为这个问题苦苦挣扎了几个小时。
这里是 Bitnami 工程师。
如果要去掉URL的/,只需要在@Karkouch的解决方案中做一个小改动。使用此配置行
RewriteRule "^/?(.*)" /index.php?url= [NC,L,QSA]
然后重启 Apache :)