简体   繁体   English

htaccess:使用silex / symfony和Web目录重定向

[英]htaccess: Using silex/symfony and web-directory redirect

I'd like to use silex with a "web"-directory for public files. 我想将silex与“ web”目录一起用于公共文件。 The structure will look like: 结构如下:

-project
--vendor
----silex
----...
--web
----index.php
--.htaccess

So I'd like to have a .htaccess script that does this tasks: 所以我想有一个.htaccess脚本来执行以下任务:

  • If I request a file ./myInstallationPath/file.php it should check if the file exist inside ./myInstallationPath/web/ and if so, redirect to it internal . 如果我请求文件./myInstallationPath/file.php,则应检查文件是否存在于./myInstallationPath/web/内部,如果存在,则将其重定向到内部。 Otherwise it should internal redirect to ./myInstallationPath/web/index.php 否则,它应该内部重定向到./myInstallationPath/web/index.php
  • If I request ./myInstallationPath/web/ it should remove the "web/" from the URL so that the user will not see the "web"-directory. 如果我请求./myInstallationPath/web/,则应从URL中删除“ web /”,以便用户看不到“ web”目录。 Then the point above should be used. 然后应使用以上几点。

Because the path of the project can be root, but also a sub-folder, the installation path could determinded the following way: 因为项目的路径可以是根目录,也可以是子文件夹,所以安装路径可以通过以下方式确定:

 RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
 RewriteRule ^(.*)$ - [E=INSTALLATION_ROUTE:%1]

This is my existing code: 这是我现有的代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:INSTALLATION_ROUTE}web/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:INSTALLATION_ROUTE}web/$1 !-d [NC]
RewriteRule (.*) %{ENV:INSTALLATION_ROUTE}web/index.php [L]

It'll check if the file does not exist inside web/ and if so, it will redirect to index.php How must the other rules look like, to meet the requirements? 它将检查文件是否在web /中不存在,如果存在,它将重定向到index.php其他规则应如何满足要求?

I recommend you to use Apache Virtual Hosts. 我建议您使用Apache虚拟主机。 My project structure looks like yours. 我的项目结构看起来像您的。 I use this in http.conf for creating the virtual host: 我在http.conf中使用它来创建虚拟主机:

<VirtualHost localhost:80>
   ServerAdmin localhost
   DocumentRoot "c:/apachefolder/htdocs/"
   ServerName localhost
   ServerAlias localhost
</VirtualHost>

<VirtualHost localhost:80>    
   DocumentRoot "C:/apachefolder/htdocs/mysilexproject/web"
   ServerName myproject.dev
   ServerAlias myproject.dev
</VirtualHost>

I added first the localhost one to still being able to access other sites like before using VirtualHosts. 我首先添加了一个本地主机,以便仍然能够像使用VirtualHosts之前一样访问其他站点。

Once you have created the VHost, you will have to set the dummy domain you have used for you project in you system host file. 一旦创建了VHost,就必须在系统主机文件中设置用于项目的虚拟域。

In case of Windows C:\\Windows\\System32\\drivers\\etc 如果是Windows C:\\Windows\\System32\\drivers\\etc

/etc/hosts for most linux distros 大多数Linux发行版的/etc/hosts

After this I put in the web directory of my project my .htaccess file that looks like this: 之后,我将我的.htaccess文件放在项目的web目录中,如下所示:

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>

Finally restart your apache to get the config changes applied. 最后,重新启动您的Apache以应用配置更改。 Now you will be able to access your site using http://myproject.dev 现在,您将可以使用http://myproject.dev访问您的站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM