zendframework 3 - service execute twice - zend-framework3

I have created a personalized service to determine the users' language.
I call my service inside the Module class of my Moduel.php like this:
$languageService = $sm->get("LanguageService");
$languageService->setLanguage();
The configuration of the service is instead found inside the global.php configuration file as well as the code below.
'service_manager' => [
'factories' => [
\Application\Service\LanguageService::class => \Application\Service\Factory\LanguageServiceFactory::class
],
'aliases'=>[
'LanguageService'=>\Application\Service\LanguageService::class
]
],
The problem using xdebug the code is executed twice (constructor and methods)
I have noticed that the code is actually executed twice. The first time the url of the request is / therefore my index. The second call is the url /css/bootstrap-select.css.map. I think it's an internal call to the plugin. I do not think the behavior is correct.

The behaviour is correct. You have a problem with your server. I do not know the fix for nginx type, but for Apache, using .htaccess you should add a bit of code that serves files from the public directory straight away instead of going through the application.
By default, the Zend Skeleton Application comes with the following .htaccess file content in the public/ directory (source):
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 ^.*$ - [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 or installed the project in a subdirectory,
# 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 [L]
The project root directory does not come with an .htaccess file. If you correctly map your server to use the project public/ directory as the projects access point, you won't need it.
Based on comment discussion, I'm assuming somewhere some config is not correct and is directing the /css/* requests to the project root. To make sure that those requests do not enter the project I have the following .htaccess in my project root:
RewriteEngine On
# If URL to the application is http://foo.com/path/to/ZendSkeletonApplication/
# the set the base to /path/to/ZendSkeletonApplication/
RewriteBase /
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Below is ZF2 default
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]
Not very pretty, but it gets the job done.

Related

Wordpress at root and vue in sub dir. Masking of vue with wordpress URL. URL points root and page load is of vue

At root there is WordPress site. And in subdirectory calles 'vue' there is vue build is there. In wordpress URL if 'podcaster' or 'crowd' is not a part of a URL then I want to load vue page without changing browser URL.
Below is my folder structure
Target is:
www.example.com/podcaster //display in the address bar, points to WP (var/www/html/)
www.example.com/crowd //display in the address bar, points to WP (var/www/html/)
www.example.com/username //display in the address bar, points to VUE (var/www/html/vue)
www.example.com //display in the address bar, points to WP (var/www/html/)
Below is the WordPress root directory .htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrite rule for vue
RewriteCond %{REQUEST_URI} !^/crowd/
RewriteCond %{REQUEST_URI} !^/podcaster/
RewriteRule (.+) /vue/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Below is vue sub directory .htaccess:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
You've not stated exactly what the problem is, however...
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteRule ^(.*)$ /vue/$1 [L,NC]
This will result in requests for the root being internally rewritten to the /vue subdirectory (I'm assuming you've placed this at the top of the WordPress .htaccess file in the root directory). You've stated that WordPress should be served from the root. In which case you should change the RewriteRule pattern from .* (0 or more) to .+ (1 or more) to avoid being triggered for requests to the root (base URL / homepage).
You will also need to ensure that rewritten requests (by the WordPress front-controller - to index.php - in the code that follows) are not also rewritten (otherwise everything will ultimately be rewritten to the /vue subdirectory). We can do this by adding another condition that checks against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to 200 (as in 200 OK status) after the later rewrite.
UPDATE: And I suspect you also have a number of static resources (CSS, JS, images, etc) that also need to be excluded, so we probably need to add a filesystem check for those, so the request is not rewritten if it already maps to a file (or directory).
For example, bringing the above points together, this becomes:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /vue/$1 [L]
I've removed the NC flag as that was superfluous here. Note also that the condition is successful when the string "/podcaster/" (note that slashes) does not appear anywhere in the URL-path. Should this not be restricted to the start of the URL-path perhaps? eg. !^/podcaster/.
Also, where is the rest of the WordPress .htaccess file? The WordPress front-controller (the part that normally appears after the # BEGIN WordPress comment marker) should appear after your custom directive. If you place your custom rewrite at the end of the WordPress .htaccess file, after the WP front-controller section then your directive will not doing anything since all requests will be routed to WordPress.
Note that you should place your custom directives before the # BEGIN WordPress comment marker. You should never edit the code between the # BEGIN WordPress and # END WordPress comments since this block of code is maintained by WordPress and your code could be overwritten when WP updates (unless you take additional steps to prevent this). There is no need to repeat the RewriteEngine or <IfModule> directives.
Below is vue .htaccess which in "vue" sub directory
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vue/
RewriteRule ^vue/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vue/index.html [L]
</ifModule>
The Vue .htaccess file isn't quite right (although should work OK). The first RewriteRule directive (which is simply an optimisation) should not include the vue subdirectory in the regex. In other words, it should be written:
RewriteRule ^index\.html$ - [L]
Since the URL-path that is matched is relative to the filesystem path that contains the .htaccess file.
In fact, you can remove all instances of vue from this file (which makes it simpler and more portable), providing you also remove the RewriteBase directive entirely. For example:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Relative substitution strings (ie. index.html in the 2nd RewriteRule directive above) are relative to the directory that contains the .htaccess file (providing you don't have a RewriteBase directive that states otherwise). So, the above naturally rewrites the request to /vue/index.html without having to explicitly state the directory.

wordpress site missing images after htaccess change

I have a wordpress installation in the root and another one in a subfolder within the root.
What would normally happen is that the url would look like:
https://example.com/quotes/us/some-url
but I wanted to remove 'quotes' from the url so it just ended up like:
https://example.com/us/some-url
Thanks to another stack overflow user, I was able to get that to work with the below htaccess code but I didn't realise that the images are now not showing and I get a 404 error for all of them. This is the root .htaccess file
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]
# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
and the subfolder 'quotes' .htaccess looks like this
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quotes/index.php [L]
</IfModule>
# END WordPress
I get a 404 error to this path https://example.com/wp-content/themes/theme_quotes/style.css?ver=1.0.0. So, it is looking in the root installation where that theme doesn't exist as it only exists in the subdirectory installation
Currently, we only rewrite requests to the /quotes subdirectory when the URL-path starts with a 2-letter language code, since that is the only thing that appears to differentiate the URLs between the two WordPress installs. However, that means that URLs to your static resources (as above) that do not have the language code prefix (and do not reference the /quotes subdirectory directly) are not being rewritten and so fail with a 404.
This could perhaps be fixed in WordPress, by including /quotes in the URL to your static resources. But that does expose the /quotes subdirectory for anybody looking at your HTML source. We would also need to modify the redirect directive in the /quotes/.htaccess file to prevent these requests being redirected back to root. EDIT: Actually, it looks like this is happening with your images already which already include the full ("correct") URL-path.
What we could do... in the root .htaccess file, rewrite any request for a static resource (image, CSS or JS file) to the /quotes subdirectory if it doesn't exist in the root. For example:
# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]
# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif)$ quotes%{REQUEST_URI} [L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
This does mean that should you have two static resources with the same name (same base URL-path) in both installations then the one in the root installation will "win".
Note that this is a "blind" rewrite... if a particular static resource does not exist in either installation then you will always get the 404 in the /quotes installation. But there's no way to really resolve that since there is an element of ambiguity in the URL-path structure.
AND, in the /quotes/.htaccess file, prevent any direct requests for static resources being redirected back to the root. For example:
# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|webp|gif)$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
I'm assuming all your file extensions (to static resources) are lowercase.
You will need to clear your browser cache, since the image redirect back to root will likely have been cached by the browser (since this is a 301 - permanent - redirect).

.htaccess : how make URL rewrite for Symfony AND Wordpress at the same time

i have one domain and one server for all my web app, my server already got a wordpress and a php app but i want to add a symfony app on it (i really understand that's not a good way to do it).
But i have troubles with my .htaccess...
My server looks like :
server_root
My wordpress works correctly, myapp1 (full php) works correctly, but myapp2 (Symfony 4) doesn't seems to work.
When i go to www.mydomain.com/myapp2/public i have the good redirection to www.mydomain.com/myapp2/public/login but with a wordpress 404 not found error ...
i'm 100% sure i have to change something in the .htaccess file but i can't figure out how making it work...
i tried to add some lines but nothing seems to work..
here is the .htaccess file of my server :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
i really need help on this ... sorry to bother with my problems,
thanks in advance
As Jenne said your current .htaccess for your Wordpress application does rewrite everything which is not
an existing directory
an existing file
to the index.php of Wordpress.
That is why your PHP application has worked fine. Symfony however needs a rewrite of everyting to its index.php too.
I do not recommend locating Symfony inside a public directory. As Jenne said you should locate it outside of it and e.g. create a own subdomain which root directory is the public directory of your Symfony application.
But you can add an extra rewrite rule to your .htaccess file which does the needed rewriting for Symfony.
# If the path points inside the myapp2 public folder
RewriteCond %{REQUEST_URI} ^/myapp2/public/.
# and there is a file at that path
RewriteCond %{REQUEST_FILENAME} -f
# use that file.
RewriteRule ^ - [L]
# If the url points to /myapp2 use the Symfony index.php.
RewriteRule ^/myapp2(/.*)?$ /myapp2/public/index.php [L]
# If the path points to something which is no existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# use the Wordpress index.php.
RewriteRule . /index.php [L]
That way you can access all files in the Symfony public directory like that:
www.example.com/myapp2/public/somefile.txt
And use everything else at /myapp2 for your Symfony routes. But all files which are in your myapp2 folder and outside of the public folder can't be accessed.
Lets do a warning first: having all these files in those public folders like this is very BAD as we can simply open the configuration files browsing to eg yourhost/myapp1/config/services.yaml. And these will be fully readable! It is therefor advised to only expose the public folder of symfony projects (the one that has the index.php) to the outside world. Or you would have to edit the htaccess even further to only allow acces to specific file types/folders like assets
There is an answer to the question though, what is going on is:
# If the requested path is NOT (!) an actual file (-f) on the disk
RewriteCond %{REQUEST_FILENAME} !-f
# And if the requested path is NOT (!) an actual directory (-d) on the disk
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite any path (`.` is a regex where the dot will match anything) to /index.php
RewriteRule . /index.php [L]
There for calling file/directory in myapp1/2 would work as they are actual file calls and wont get forwarded to the root/index.php (wordpress).
We can solve this by adding
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/myapp1
RewriteCond %{REQUEST_URI} !^/myapp2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The first two can also combined in regex using RewriteCond %{REQUEST_URI} !^/(myapp1|myapp2)
This results in any path:
NOT starting with /myapp1
AND NOT starting with /myapp2
AND IS NOT a file
AND IS NOT a directory
being rewritten to /index.php.
You can then also add a .htaccess in myapp1/2 to do the rewriting for those apps specifically.

Using .htaccess to redirect example.com/about.php to example.com/about/

I recently switched over my website to Wordpress. My old files were www.example.com/about.php and now its www.example.com/about/. I need to redirect incoming links from the .php extension to just the / for ALL my pages preferably using .htaccess.
What I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Whats in my .htaccess file:
# 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
I guess I don't know where I'd put it in my .htaccess file either.
Thanks!
The code you have does not redirect anything. It takes a request that might be for a php file and internally appends the .php extension. Nothing happens on the browser because you've not told it to do anything. This is a 2 step process here. See the top part of this answer for a short explanation.
In order to redirect, you need to match against the incoming request, not the URI (which could have been rewritten be previous rules or iterations):
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /(.*)\.php($|\ )
RewriteCond %{REQUEST_URI} !wp-admin
RewriteRule ^ /%2/ [L,R=301]
So when someone types http://www.example.com/about.php in their browser's URL address bar, the request will look like:
GET /about.php HTTP/1.1
and the %2 backreferences about and redirects the browser to http://www.example.com/about/ (note the trailing slash) and the address bar changes.
What happens then is the browser makes ANOTHER request but this time for http://www.example.com/about/ and the server gets the URI /about/. Now you need your rule to internally rewrite it back to the php file. Unfortunately, your rule doesn't handle the trailing slash, so you need something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
These would all go before your wordpress rules. The wordpress rules route everything to index.php and that would wreck any URI you are trying to rewrite.
You are doing things the wrong way around. You are actually rewriting urls that end end without php to files that do end in .php.
You'd need to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).php$ $1/ [NC]
I also removed the check if the file you are redirecting to exists as well as the [L] flag, because it's likely that wordpress is also doing its own rewriting, which means that you don't want this to be the last rule processed and that you won't be able to find the file on the filesystem.

how to run wordpress and zendframework from the same docroot

I have a site running in wordpress in shared hosting terria.net
I have developed another project in zend framework and have deployed under a tools directory,
I would like to access it www.abc.com/tools.
I have copied the content of .htaccess mentioned by Lorenzo Albertons in tools/.htaccess.
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
But the url is not properly redirected to the tools project rather it is treating it as a wordpress page.
If www.abc.com points to your ZF app's public directory, then anything you put in there should be accessible via the appropriate URL segment using Zend's default .htaccess settings (the re-writing only kicks in if there isn't a physical folder or file already present).
We run a Wordpress site in our ZF application by just adding a symlink to the blogfolder into the public directory, and access it via wwww.abc.com/blog.
For the record, we're using the follow rewrite rules (which I believe are default if you're use Zend's project generator, or at least were in ZF 1.8):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Resources