-
-
Save alch/7766993 to your computer and use it in GitHub Desktop.
# For a symfony application to work properly, you MUST store this .htaccess in | |
# the same directory as your front controller, index.php, in a standard symfony | |
# web application is under the "public" project subdirectory. | |
# Use the front controller as index file. | |
DirectoryIndex index.php | |
# Uncomment the following line if you install assets as symlinks or if you | |
# experience problems related to symlinks when compiling LESS/Sass/CoffeScript. | |
# Options +FollowSymlinks | |
# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve | |
# to the front controller "/index.php" but be rewritten to "/index.php/index". | |
<IfModule mod_negotiation.c> | |
Options -MultiViews | |
</IfModule> | |
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
# This RewriteRule is used to dynamically discover the RewriteBase path. | |
# See https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule | |
# Here we will compare the stripped per-dir path *relative to the filesystem | |
# path where the .htaccess file is read from* with the URI of the request. | |
# | |
# If a match is found, the prefix path is stored into an ENV var that is later | |
# used to properly prefix the URI of the front controller index.php. | |
# This is what makes it possible to host a Symfony application under a subpath, | |
# such as example.com/subpath | |
# The convoluted rewrite condition means: | |
# 1. Match all current URI in the RewriteRule and backreference it using $0 | |
# 2. Strip the request uri the per-dir path and use ir as REQUEST_URI. | |
# This is documented in https://bit.ly/3zDm3SI ("What is matched?") | |
# 3. Evaluate the RewriteCond, assuming your DocumentRoot is /var/www/html, | |
# this .htaccess is in the /var/www/html/public dir and your request URI | |
# is /public/hello/world: | |
# * strip per-dir prefix: /var/www/html/public/hello/world -> hello/world | |
# * applying pattern '.*' to uri 'hello/world' | |
# * RewriteCond: input='/public/hello/world::hello/world' pattern='^(/.+)/(.*)::\\2$' => matched | |
# 4. Execute the RewriteRule: | |
# * The %1 in the RewriteRule flag E=BASE:%1 refers to the first group captured in the RewriteCond ^(/.+)/(.*) | |
# * setting env variable 'BASE' to '/public' | |
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$ | |
RewriteRule .* - [E=BASE:%1] | |
# Sets the HTTP_AUTHORIZATION header removed by Apache | |
RewriteCond %{HTTP:Authorization} .+ | |
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0] | |
# Removes the /index.php/ part from a URL, if present | |
RewriteCond %{ENV:REDIRECT_STATUS} ="" | |
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L] | |
# If the requested filename exists, simply serve it. | |
# Otherwise rewrite all other queries to the front controller. | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule ^ %{ENV:BASE}/index.php [L] | |
</IfModule> | |
<IfModule !mod_rewrite.c> | |
<IfModule mod_alias.c> | |
# When mod_rewrite is not available, we instruct a temporary redirect | |
# to the front controller explicitly so that the website | |
RedirectMatch 307 ^/$ /index.php/ | |
</IfModule> | |
</IfModule> |
Working on 2023 !
You saved my life (or at least my brain) ! thank you !!
I added the following so it will also go to any existing directory in public: (if that is what you want your application to do)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:BASE}/index.php [L]
Thank you, that saved precious time!
Maybe a nice addition would be a redirect of http to https, perhaps commented out by default
also working with symfony 7
thank you
I added a condition to the .htaccess file as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^uploads/([a-zA-Z0-9_]+)/([^/]+)/([0-9]+x[0-9]+)/(.+)\.(jpg|jpeg|png|gif)$ /generate_thumbnail?directoryMain=$1&path=$2&resize=$3&storedName=$4&extension=$5 [QSA,L] - [L]
I want this to handle generating image thumbnails. When I run the URL http://project.dev.localhost.com/uploads/service_pack/09-2024/275x156/f675f980b33e7f835f7efc8ebd740d6468611da31_11.png, I expect it to redirect to the route I desire, but I always get redirected to a 404 page. I have configured Apache and set the appropriate folder permissions. Can anyone help me? Thank you.
@bastoune I updated the gist with a shorter and more updated version, based on the official Symfony Apache Pack