Skip to content

Commit 5fa22c5

Browse files
committed
merged branch Tobion/htaccess (PR #503)
This PR was merged into the 2.2 branch. Commits ------- ea255c0 made the startpage work when mod_rewrite is not available e97e7fa redirect /app.php to prevent duplicate content Discussion ---------- redirect /app.php to prevent duplicate content Improved version of #497. This time it's compatible with Apache < 2.3.9 by not using the END flag. It also features default compatibility with apache aliases (inspired by [Zend](https://github.com/zendframework/ZendSkeletonApplication/blob/master/public/.htaccess)). And it improves the rewriting performance for the homepage by preventing the process to apply to each DirectoryIndex file. Now I also could also make the startpage work when mod_rewrite is not available. I tested it thoroughly and also debugged apache rewriting to see what's happening. It was quite hard to find out how to achieve all that. [This resource](http://stackoverflow.com/questions/7798099/how-to-block-multiple-mod-rewrite-passes-or-infinite-loops-in-a-htaccess-cont) helped quite a bit. May be good if someone else can confirm all works. Example behavior for each scenario: With mod_rewrite: - `/` -> internally rewritten to use front controller (`app.php`) - `/path` -> internally rewritten to use front controller - `/app.php/path` -> external permanent redirect to `/path` to prevent duplicate content - `/app.php` or `/app.php/`-> external permanent redirect to `/` to prevent duplicate content - `/app.phpF` stays as-is Without mod_rewrite but with mod_alias (default module): - `/` -> external temporary redirect to `/app.php/` so it's explicit and all generated links on the startpage also work with the base path prepended - `/path` -> stays as-is and would probably return a 404 (we cannot safely and easily redirect with mod_alias to `/app.php/path` because we don't know if the request references a real file like CSS) - `/app.php/path` -> stays as-is and works because the front controller is given - `/app.php` -> stays as-is and works because the front controller is given In an aliased environment with mod_rewrite: - `/myproject/` -> internally rewritten to use front controller (`app.php`) - `/myproject/path` -> internally rewritten to use front controller - `/myproject/app.php/path` -> external permanent redirect to `/myproject/path` to prevent duplicate content - `/myproject/app.php` -> external permanent redirect to `/myproject/` to prevent duplicate content In an aliased environment without mod_rewrite: - `/myproject/` -> stays as-is and works because it uses the DirectoryIndex definition (but the links will not work) - `/myproject/path` -> stays as-is and would probably return a 404 (except there is such a file) - `/myproject/app.php/path` -> stays as-is and works because the front controller is given - `/myproject/app.php` -> stays as-is and works because the front controller is given --------------------------------------------------------------------------- by colinfrei at 2013-03-01T12:10:59Z Tested as well on Apache 2.2.16 --------------------------------------------------------------------------- by Tobion at 2013-03-01T12:42:58Z ping @gimler @vxf @neilferreira considering you commented on the intial PR --------------------------------------------------------------------------- by gimler at 2013-03-01T12:51:35Z it works for me server version: Apache/2.2.22 (Ubuntu) --------------------------------------------------------------------------- by Tobion at 2013-03-01T14:33:20Z @gimler did you also test it in an aliased environment? --------------------------------------------------------------------------- by gimler at 2013-03-01T14:38:27Z no only simple vhost --------------------------------------------------------------------------- by jalliot at 2013-03-02T15:48:08Z @Tobion if it is merged here, you should also probably submit an equivalent PR to symfony's [ApacheMatcherDumper](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php). --------------------------------------------------------------------------- by Tobion at 2013-03-02T21:11:40Z @jalliot you are right. Thanks for the hint. The apache dumper is also quite strange. For example if you request it with `/app.php/...` the dumped routes wont even be used. --------------------------------------------------------------------------- by Tobion at 2013-03-21T14:27:51Z Now I also could also make the startpage work when mod_rewrite is not available. I also updated the examples in the PR description. @fabpot it's tested and should be ready.
2 parents fe994eb + ea255c0 commit 5fa22c5

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

web/.htaccess

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1+
# Use the front controller as index file. It serves as fallback solution when
2+
# every other rewrite/redirect fails (e.g. in an aliased environment without
3+
# mod_rewrite). Additionally, this reduces the matching process for the
4+
# startpage (path "/") because otherwise Apache will apply the rewritting rules
5+
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
6+
DirectoryIndex app.php
7+
18
<IfModule mod_rewrite.c>
29
RewriteEngine On
310

4-
#<IfModule mod_vhost_alias.c>
5-
# RewriteBase /
6-
#</IfModule>
11+
# Redirect to URI without front controller to prevent duplicate content
12+
# (with and without `/app.php`). Only do this redirect on the initial
13+
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
14+
# endless redirect loop (request -> rewrite to front controller ->
15+
# redirect -> request -> ...).
16+
# So in case you get a "too many redirects" error or you always get redirected
17+
# to the startpage because your Apache does not expose the REDIRECT_STATUS
18+
# environment variable, you have 2 choices:
19+
# - disable this feature by commenting the following 2 lines or
20+
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
21+
# following RewriteCond (best solution)
22+
RewriteCond %{ENV:REDIRECT_STATUS} ^$
23+
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
24+
25+
# If the requested filename exists, simply serve it.
26+
# We only want to let Apache serve files and not directories.
27+
RewriteCond %{REQUEST_FILENAME} -f
28+
RewriteRule .? - [L]
29+
30+
# The following rewrites all other queries to the front controller. The
31+
# condition ensures that if you are using Apache aliases to do mass virtual
32+
# hosting, the base path will be prepended to allow proper resolution of the
33+
# app.php file; it will work in non-aliased environments as well, providing
34+
# a safe, one-size fits all solution.
35+
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
36+
RewriteRule ^(.*) - [E=BASE:%1]
37+
RewriteRule .? %{ENV:BASE}app.php [L]
38+
</IfModule>
739

8-
RewriteCond %{REQUEST_FILENAME} !-f
9-
RewriteRule ^(.*)$ app.php [QSA,L]
40+
<IfModule !mod_rewrite.c>
41+
<IfModule mod_alias.c>
42+
# When mod_rewrite is not available, we instruct a temporary redirect of
43+
# the startpage to the front controller explicitly so that the website
44+
# and the generated links can still be used.
45+
RedirectMatch 302 ^/$ /app.php/
46+
# RedirectTemp cannot be used instead
47+
</IfModule>
1048
</IfModule>

0 commit comments

Comments
 (0)