Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit e97e7fa

Browse files
committed
redirect /app.php to prevent duplicate content
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). And it improves the rewriting performance for the homepage by preventing the process to apply to each DirectoryIndex file
1 parent 113df25 commit e97e7fa

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

web/.htaccess

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
<IfModule mod_rewrite.c>
22
RewriteEngine On
33

4-
#<IfModule mod_vhost_alias.c>
5-
# RewriteBase /
6-
#</IfModule>
4+
# This reduces the matching process for the startpage (path "/") because
5+
# otherwise apache will apply the rewritting rules to each configured
6+
# DirectoryIndex file needlessly (e.g. index.php, index.html, index.pl).
7+
DirectoryIndex
78

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

0 commit comments

Comments
 (0)