Symfony2 - How can I remove 'web/app_dev.php/' from my url? - php

How can I remove 'web/app_dev.php/' from my url when I want my symfony website to go live?
This is the default url during development,
http://{localhost}/my-symfony-2/web/app_dev.php/hello/World
So when I go live, I would like to be able to use this url to access my symfony website,
http://my-symfony-2.com/hello/World
Any ideas?

To hide app.php you need to:
having access at least to the web root of your site. Usually on the control panel of your web hosting space you can find from which you can upload your files (or if you have the access credentials you can install and use a Free FTP client like Filezilla).
checking if you have the mod_rewrite module installed and enabled in Apache looking the phpinfo() under "apache2handler" ---> "Loaded Modules" directory (you should have that possibility directly through the control panel).
After these checks you have to:
NOTE: Symfony2 already comes with an .htaccess file stored in the default web directory but if you don't know what are you doing it's better to replace the directives containet within the "IfModule mod_rewrite.c" with those shown below.
Use the native Symfony2 .htaccess or create a new one file with the .htaccess extension and copy/paste inside these directives to hide app.php in production (not on localhost):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteRule .? - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
# and from the symfony's original
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
To apply the modifications you need to restart Apache (also this is usually did on the control panel of your web space).
Googling a bit you can find tons of examples and tutorials but to start to learn take a look at this simple .htaccess guide with many useful infos and examples and the Official URL Rewriting Guide (Apache2.4). If you encounter some problem update your post adding all related infos otherwise you can make another question here on SO.

If you have access to the deployment server, from terminal you can use:
php app/console cache:clear --env=prod and this will clear the prod cache and you will be able to use the standard route.
But if you are deploying the application on hosting where you haven't access to terminal, need to find the app/cache/* and app/logs/* and remove their content and after add the proper rights (766|777) for the folders.

Related

There is "/?i=2" in my wordpress website index url

I recently grabbed a host and first there was an index2.html in my htdocs root which was default index, I didn't remove that until I install wordpress in that root. then wordpress automatically made a httaccess file with following codes which is normal:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
now I realize that sometimes there is /?i=1 or /?i=2 in my general url, for example, wordpresssite.com/?i=2
this is a problem because I'm using some ad-unit system and I gave my website url to the system but it seems that the ad system bots some time redirect to "wordpresswebsite.com/?i=2" url and it does not match with the url I entered before (wordpresswebsite.com) so it's not gonna work.
now I removed the index2.html but the problem is still there.
this ?i=1 or 2 is about PHP GET parameter but how this parameter will appear.
I found this on reddit with similar issue and the conclusion was:
Most likely there are some rules at vhost level.
---- yes it's from the host, they said they cannot disable it as it's protecting me from bots.
For those interested in removing the /?i=1 installed mostly by free host providers here's a tip.
Edit & upload the .htaccess file in root of website
RewriteCond %{QUERY_STRING} ^(.*)\?i=1$ [NC]
RewriteRule ^(.*)$ /$1?%1 [R=301, L]
If you're using Apache, you have to enable mod_rewrite first.
sudo a2enmod mod_rewrite
Then restart or reload your server for the new configuration to take place:
sudo service apache2 restart
(Try to find some fancy control panel option for this, if not having shell access)
?i=1 or 2 or 3 is to protect your website from bots. Try using cloudflare for your website or use the .htaccess code to remove ? paramaters. Note that using any code to remove ?i=1 will remove all ? parameters.

ZF2 Apache install on VPS witout creating Virtual Host

I want to install my ZF2 application on a VPS server without support for Virtual Host. I´m using a simple application based on ZendApplicationSkeleton.
I´m using the default .htaccess:
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
All solution I´ve found in SO does not work for me. They may fit ZF1, but not ZF2:
Link 1
Link 2
Link 3
Link 4
My application is in a folder named /var/www/html/testapp.
The main page is loaded once I typelocalhost/testapp/public on the browser. Also my module is loaded if I type localhost/testapp/module, but navigation does not work.
Ie: in the main page, I´ve created a button like:
Go To Module
But if I click on it I navigate to localhost/login/index showing Not Found, not to the correct module/index.phtml page.
Help appreciated with that.
Your problem has nothing to do with server configuration. Since your app is in a sub-folder, the link is wrong. It would need to be something like
Go To Module
for it to work. However, public/ should never appear in you URLs. With things setup this way you are allowing users to view files outside your app's web root, which is a potential security risk (and results in ugly URLs).
The solution to this is to setup a separate vhost for your ZF2 app, which has a DOCUMENT ROOT pointing at the app's public folder. If you are having problems with this, post that as your question; or if you can explain why this isn't possible perhaps we can advise further.

Trouble redirecting to sub-directory using apache's `mod_rewrite`

I am trying to update a legacy web application, so I'm trying to set up both the legacy application and the updated version in parallel, and gradually migrate to the new one.
The old application is located directly under public_html, and have put the new Application under public_html/symsale, now I want all requests directed to the new application to be rewritten and a web subdirectory added after symsale, i.e: a url like /symsale/path, must be rewritten to /symsale/web/path. for that I've create a .htaccess file and put it under the symsale directory. The contents follow:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)(symsale)(.*)$ $1$2/web$3 [L] # i've tried it both with and without the 'L'
</IfModule>
When I try to navigate to /symsale/ I get the following error
You don't have permission to access /symsale/ on this server.
However when I use /symsale/web instead, it works just fine. What's the problem here?
Note: I have seen these other questions: Apache rewrite rule to redirect all request to subdirectory containing another .htaccess and rewrite rules, Apache RewriteRule to subdirectory, .htaccess rewrite to redirect root URL to subdirectory
Note: I'm using the symfony framework (php) so there is another .htaccess file inside the web folder, which rewrites all routes to the front controller. The contents of that .htaccess follow.
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
Here is one longshot -- mod_rewrite has an unfortunate/quirky behavior where it guesses whether you're substituting a filesystem path or a URL path. It guesses by looking at the first component of the substitution and seeing if it exists as a directory on disk (absolute path)
Do you happen to have a /symsale/ at the root of your filesystem? I believe [PT] will short-circuit this guessing and force the result to be treated as a URI.

Symfony profiler throws 404

I've created new project in symfony and I get an error prompt on each site:
An error occurred while loading the web debug toolbar (404: Not Found).
Do you want to open the profiler?
When I open the profiler there is a message
Token not found
Token "59942c" was not found in the database.
Moreover, in the cache directory the profiler directory is empty! I checked permissions and they're proper. I tried cache:clear and removing cache manually, but that didn't help.
It's often problem with event subscriber/listener.
Try run
console debug:event
And check for errors
This usually happens if the cache directory is not properly set up to allow the web servers' user to write data into them.
Under Linux, I would usually use this for development:
php app/console --env=dev cache:clear
php app/console --env=dev cache:warmup
chmod -R a+rwX app/cache/
setfacl -Rm g:www-data:rwX app/cache/
setfacl -Rm g:dev-user:rwX app/cache/
This ensures all required directories will be created and then assigned the required permissions. If you do not have ACL enabled, skip the setfacl commands.
OFC, replace www-data with your web servers' username, and dev-user with your username.
I had the same issue, it was caused because was using Apache without an .htaccess file.
If you are using Apache locally or on your server you can install Apache Pack to automatically generate an .htaccess file:
composer require symfony/apache-pack
This pack installs a .htaccess file in the public/ directory that contains the rewrite rules needed to serve the Symfony application.
https://symfony.com/doc/current/setup/web_server_configuration.html
This is a known bug in the profiler. Since Symfony 2.4, the profiler persisting the data used by the WDT is the very last thing done by Symfony.
The alert you see means that the profiler has not been able to find the token requested in the 2500ms allowed, However if you click OK in the alert, you should be redirected to the profiler and figure out what precisely is slow.
Sometimes, on big projects (custom Sylius app in my case), profiler data generates very slowly.
Symfony checks things only 5 times each 1000 ms, so if data was not generated during 5 seconds - you will get An error occurred while loading the web debug toolbar.
To understand whether this is a case - open _wdt url at new tab - if it 404, but after some time - 10-15-30 seconds it become 200 - then this is a case.
To fix this, you should override vendor/symfony/web-profiler-bundle/Resources/views/Profiler/base_js.html.twig (to templates/bundles/WebProfilerBundle/Profiler/base_js.html.twig) and replace { maxTries: 5 } to { maxTries: 30 }.
After that, script will be trying to get profiler / web debug toolbar page for 30 seconds which hopefully will be enough for your app to generate profiler data:
I had the same issue after upgrading Symfony 2.3->2.6. After upgrading web/app_dev.php it fixed the issue. I believe you need the Debug::enable(); line.
Need .htaccess file for Apache. I am not sure, why there is no default in Symfony like Laravel. This one if copy from Laravel and help with this problem, too.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Had the same issue after upgrading 2.6 -> 2.8 on a 2.4 era project. Got a useMicrosecondTimestamps error with the node layer. Fixed by nuking rm -r app/cache/*. #marenkay's method did not clear the node-php server cache.
Try to edit or add your htacces file like this
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# 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
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
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 of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
If you do not get 404 every time, but periodically and use docker at the same time, then you should check that you do not have conflicts with the containers accepting requests. In my case, several projects were launched with the same name "fpm" and, of course, only one of them had a real token

Laravel in subfolder on webhost

I have a webapp written in Laravel that needs to run in a folder on a web host.
The app will have to be accessible via hostname.com/webhit/. This will point to the app's home page.
I only have one route:
Route::controller('/', 'HomeController');
HomeController's getIndex needs to serve the home page. This works.
However, as soon as I want to go to something like hostname.com/webhit/login, I get a 404 from Apache.
Obviously, .htaccess is not working properly. I need it to, essentially, turn URLs that look like hostname.com/webhit/login into hostname.com/webhit/index.php/login.
I have a .htaccess file in www/webhit (where index.php is located) that looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ webhit/index.php/$1 [L]
</IfModule>
I am not very familiar with .htaccess file syntax, but I believe it's doing something wrong.
Edit:
I did it. My .htaccess was wrong (it actually causes a redirect loop), but the issue was that it wasn't even being parsed by Apache (hence the 404 instead of a 500 due to >10 redirects in a request). I did the following steps in order to get everything to work:
Enable mod_rewrite and restart Apache (plenty of docs out there on how to do this)
But wait, there's more! By default, Apache on Ubuntu prevents URL rewrites. See this site. Most importantly, the following fragment from the URL above is very important: "By default, Ubuntu's Apache will ignore the directives in your .htaccess files." You will need to actually enable rewrites by editing \etc\apache2\sites-available\default and setting AllowOverride to all (see link above for more details).
Reload the configuration (or just restart apache).
Make sure you're using the correct .htaccess. My original version actually has a redirect loop in it. See the selected response for the correct version.
After this, I got it to work. I hope it helps future programmers having a similar issue!
Check default server requirements - laravel .htaccess file works for most situations. Try with this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I would suggest you to use resourceful controllers - mappings from your route to your controllers methods are much more clear, and you'll get full
resource with one command (routes,models,views,controllers)

Categories