I hope that this question is not a duplicate, but I only found articles or questions about this for old versions of Symfony. I am new to Symfony 4 and I would like to create a new application with it and install it as a part of an already existing website (domain below). The Sf 4 app goes into the admin/ subdirectory shown in the hierarchy below. domain uses the TYPO3 CMS, but I’m not sure that it matters for this question.
First, here is the directory structure :
/home/webuser/domain/
public/ (this is the document root for "domain.localhost")
admin/ (the Symfony app goes here)
public/
…
…
typo3/
.htaccess
index.php
…
composer.json
I am currently working on the website on my local machine and I would like to access the Symfony application root from domain.localhost/admin. I am using an Apache virtual host and the domain/public folder is the document root of domain :
<VirtualHost *:80>
ServerName domain.localhost
DocumentRoot "/home/webuser/www/domain/public"
</VirtualHost>
I tried adding the following rewrite rule to my VirtualHost :
<VirtualHost *:80>
ServerName domain.localhost
DocumentRoot "/home/webuser/www/domain/public"
RewriteEngine On
RewriteRule "^/admin/(.*)$" "/admin/public/$1"
LogLevel trace1
</VirtualHost>
But this seems to work only when I visit http://domain.localhost/admin/ : I then see the "Welcome to Symfony 4.2.7" page. As soon as I try to visit a subpage, I get a 404 error. I tried to create my first page at lucky/number as explained in the documentation here, and looking at the Apache error log, I have lines such as :
[Mon Apr 29 18:52:09.472043 2019] [rewrite:trace1] [pid 22250:tid 140404453660416] mod_rewrite.c(483): [client ::1:43084] ::1 - - [domain.localhost/sid#55a33c704bf8][rid#7fb260002bd0/initial] [perdir /home/webuser/www/domain/public/] pass through /home/webuser/www/domain/public/admin/public/lucky
[Mon Apr 29 18:52:09.472087 2019] [core:info] [pid 22250:tid 140404453660416] [client ::1:43084] AH00128: File does not exist: /home/webuser/www/domain/public/admin/public/lucky/number
Now, TYPO3 has an .htaccess file containing URL rewrites, but I don’t think that they are interfering. Below, I just added myself the admin/ folder in the rule after the comment that starts with "Stop rewrite processing", so that we are not redirected to the home page of the domain website when the file is not found :
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
# Store the current location in an environment variable CWD to use
# mod_rewrite in .htaccess files without knowing the RewriteBase
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=CWD:%2]
# Rule for versioned static files, configured through:
# - $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename']
# - $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ %{ENV:CWD}$1.$3 [L]
# Access block for folders
RewriteRule _(?:recycler|temp)_/ - [F]
RewriteRule fileadmin/templates/.*\.(?:txt|ts)$ - [F]
RewriteRule ^(?:vendor|typo3_src|typo3temp/var) - [F]
RewriteRule (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ - [F]
# Block access to all hidden files and directories with the exception of
# the visible content from within the `/.well-known/` hidden directory (RFC 5785).
RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule (?:^|/)\. - [F]
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule ^(?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|admin/) - [L]
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]
</IfModule>
The production server will be a dedicated system, so I can modify anything I want.
It turns out that I just needed to install the Apache pack, like in this answer
Related
In our AngularJS project I have a set of HTML templates for reports that are currently part of the deployment but this means that whenever our client requests a change to one of the HTML templates I need to make update the complete application.
Is it possible to do the following for a request for these HTML templates:
Assume the request for assets/reports/report1.html.
Use the file <external-dir>/assets/reports/report1.html if that file exists. The external directory is not directly accessible from the internet.
Otherwise, use the initial request.
Most mod_rewrite solutions I could find seem to stop after the first rewrite.
Update: I've added the configuration changes below to the complete virtual host definition but the Alias seems to disrupt things:
<iFmODule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
Alias /advisor/report /var/www/html/report
Alias /report-templates /opt/reports
#<Directory /var/www/html/report>
RewriteEngine On
# Preventing direct access to /report-templates
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/?report-templates(/.*)?$ - [R=404,L]
# If the request was not already rewritten,
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# and the file do really exist in /advisor/report/assets/reports
RewriteCond %{REQUEST_FILENAME} -f
# rewrite the request from /advisor/report/assets/reports to /report-templates
RewriteRule ^/?advisor/report/assets/reports/(.*)$ /report-templates/$1 [L]
# If the file do not exist in /report-templates/
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite the request back
RewriteRule ^/?report-templates/(.*)$ /advisor/report/assets/reports/$1 [L]
#</Directory>
LogLevel warn rewrite:trace8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
There are 2 AngularJS applications, the advisor portal located under /var/www/html/advisor and the reporting application located under /var/www/html/report. The latter is accessed through https:/example.com/advisor/report and is the reason for the Alias.
I changed the Alias to:
<Directory /var/www/html/advisor>
RewriteEngine On
RewriteRule ^/?advisor/report/(.*)$ /report/$1 [L]
</Directory>
This way the Alias should not interfere. After this I enabled the <Directory> directive for /var/www/html/report and removed the advisor/ part from the rewrite rules and conditions but I didn't see the rewrite happening in the logs.
I assume you have a directory called external-dir in your root (public_html) folder.
If you have full access to your server, this directory could be also somewhere else on the file system, but than you have to add a Alias in your Virtualhost configuration.
Alias "/external-dir" "/absolute/path/to/external-dir"
and the Apache Module mod_alias must be enabled:
sudo a2enmod alias
Now the main Part: This procedure will work for all existing files in the folder (or sub folder) /assets. First we check if the same file exist in /external-dir/assets and if so we serve it, if not we serve the original one. Its not possible to get a file from /external-dir/assets that do not exits in /assets. If you need more restrictions you can add them as your need it.
You can put this it in a .htaccess file in your root (public_html) folder or in your Virtualhost configuration, than I would use a Directory Direktive for your root directory
RewriteEngine On
# Preventing direct access to /external-dir
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/?external-dir(/.*)?$ - [R=404,L]
# If the request was not already rewritten,
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# and the file do really exist in /assets
RewriteCond %{REQUEST_FILENAME} -f
# rewrite the request from /assets to /external-dir/assets/
RewriteRule ^/?assets/(.*)$ /external-dir/assets/$1 [L]
# If the file do not exist in /external-dir/assets/
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite the request back
RewriteRule ^/?external-dir/assets/(.*)$ /assets/$1 [L]
I tested this positive on an Ubuntu 16.04.03 LTS Server with Apache 2.4.27.
I wrote a content management system that uses rewrite rules to map urls to controller, action and argument query strings.
I use two .htaccess files. One is in my site's root directory. This one forwards all requests to a sub directory, which depends on the domain name used for the request:
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
# mapp requests that don't start with www to https://www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# BEGIN Domain to folder mapping
########################
# pointing example.com to subfolder 'example'
ReWriteCond %{HTTP_HOST} (www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
ReWriteRule ^(.*)$ example/$1 [L]
# END Domain to folder mapping
A second .htaccess file that maps the request to the actual query string is in the subdirectory that gets mapped to in the first rewrite step:
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]*)([/]*)([^/]*)([/]*)(.*) index.php?controller=$1&action=$3&args=$3 [L,QSA]
The result of all this, before I installed my ssl certificates, was that a request that looks like this:
example.com/hello/foo/123
would map to:
http://www.example.com?controller=hello&action=foo&args=123
Now I just get sent to the index file. If I enter the actual query string, I get the correct result.
I looked through my server's rewrite logs and it looks like there is an additional rewrite step that gets executed without me being able to figure out why. I think this is the relevant stuff from the rewrite logs:
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] strip per-dir prefix: /fs6c/example/public/example/hello -> hello
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] applying pattern '([^/]*)([/]*)([^/]*)([/]*)(.*)' to uri 'hello'
[rewrite:trace2] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] rewrite 'hello' -> 'index.php?controller=hello&action=&args='
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] split uri=index.php?controller=hello&action=&args= -> uri=index.php, args=controller=hello&action=&args=
I am assuming the [L] flag is the culprit, but I don't understand why this wasn't an issue before I switched to ssl.
I am using a shared hosting service, and they had to install the certificates for me, since they don't expose the necessary parts of the server to the user.
I still don't know WHY this is happening, but I was able to find a way to make it work with the ssl certificate. These are the relevant rewrite rules in the htaccess file located in the root directory:
# for existing files, redirect to subdirectory
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
RewriteCond %{DOCUMENT_ROOT}/example/$1 -f
ReWriteRule ^(.*) example/$1 [L]
# for non-existing files, map to index.php with CMS arguments
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com
# file exists, but is index.php
ReWriteCond %{REQUEST_URI} index.php [OR]
# file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^([^/]*)([/]*)([^/]*)([/]*)(.*) example/index.php?controller=$1&action=$3&args=$5 [L,QSA]
I eliminated the second htaccess file.
I hope this helps someone in the future. The hosting provider is NearlyFreeSpeech. If anyone has any clue why my old rules didn't work, I would really appreciate to know!
I am trying to migrate my testing site, locally.
I am running WAMP with:
Apache 2.2.22
Php 5.3.13
I have added to hosts file:
127.0.0.1 local.testsite.com
I have added to httpd-vhosts.conf (and included it in httpd.conf):
<Directory C:\wamp\www\testsite>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:\wamp\www\testsite"
ServerName local.testsite.com
</VirtualHost>
And rewrite_module is ON.
I am getting the following error in my local apache error log:
[Sun Oct 26 09:24:17 2014] [alert] [client 127.0.0.1]
C:/wamp/www/testsite/.htaccess: RewriteCond: bad flag delimiters,
referer: http://local.testsite.com/
This is my htaccess file (which worked on my external server):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dashboard/$ / [R=301,L]
RewriteRule ^dashboard$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
Any ideas what could be the problem with my local wamp setup?
Thanks.
I had removed a ton of blank lines from the file (for aesthetic purposes only), when I undid this the htaccess file worked perfectly.
I am trying to use TYPO3 on a WAMP system but i'm having problems with the rewritten URLs.
I have installed the introduction package which has a "get-started" website. Everytime i try to access the website through one of these:
localhost/typo3
localhost/typo3/index.php
localhost/typo3/index.php/get-started
the url becomes localhost/typo3/get-started
Which is okay and it means mod_rewrite is on and working. The problem is that i can't see the site localhost/typo3/get-started and i have an "Object not found" page instead.
I have the same issue on the same machine with Symfony 1.4 but i never cared about that because on Symfony i can use the frontend_dev.php page to access the site (on my production environment rewritten URLs work fine instead).
This is the httpd.conf entry for the TYPO3 directory:
Alias /typo3 "C:\workspace\typo3"
<Directory "C:\workspace\typo3">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
And this is the .htaccess file (which was already inside the TYPO3 package and i haven't modified), i have removed the non related parts
### Begin: Settings for mod_rewrite ###
# You need rewriting, if you use a URL-Rewriting extension (RealURL, CoolUri, SimulateStatic).
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
# Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
#RewriteBase /
# Rule for versioned static files, configured through:
# - $TYPO3_CONF_VARS['BE']['versionNumberInFilename']
# - $TYPO3_CONF_VARS['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
# Redirect http://example.com/typo3 to http://example.com/typo3/index_re.php and stop the rewrite processing.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/typo3$ /TYPO3root/typo3/index.php [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
# For httpd.conf, use this line instead of the next one:
# RewriteRule .* /TYPO3root/index.php [L]
RewriteRule .* index.php [L]
</IfModule>
### End: Settings for mod_rewrite ###
Solved, the problem was on the easyphp settings because i wasn't using its DocumentRoot (but just using aliases), changing the DocumentRoot to the one where i keep my projects solved it
You should change the #RewriteBase / to RewriteBase /typo3/ as described inside the .htaccess file.
I read here that the ".git folder is at the root level of the web site, and is probably publicly accessible. To protect the folder and prevent unwanted clones of the repository, add the following to your top-level .htaccess file to forbid web access:"
# deny access to the top-level git repository:
RewriteEngine On
RewriteRule \.git - [F,L]
First of all, THANKS Joe Maller!
In my virtual host file i have the RewriteEngine On command with the following specs:
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.mydomain.com/ [R]
</VirtualHost>
Could i just add the git rewrite rule following the rewrite rule for ssl like so:
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.mydomain.com/ [R]
RewriteRule \.git - [F,L]
</VirtualHost>
Thanks!
That should work. It's easy to test to make sure, though. Just try to browse to http://www.mydomain.com/.git and see if it works.