how to fix css loading problem on production server - css

my website is now hosted on a server and the URL is like this
test1.test.com
but now I have to move it to another hosting which they don't have this ability and I had to change it to:
test.com/test1
until now everything is working except the CSS and JS files I get 404 not found error.
and in my case, I didn't change anything and this is how I am loading the CSS files
<link rel="stylesheet" href="{{ asset('css/main.css') }}">
does anyone have any idea how can I fix this?
of course, i tried to add the (test1) before the {{ asset('css/main.css') }} but it didn't change anything.
PS: the source code is not saved under sub directory the /test1 is just routing to a load balancer.
and here are the apache2 setting file for docker image:
<VirtualHost *:80>
DocumentRoot /var/www/html/web
<Directory /var/www/html/web>
AllowOverride None
Require all granted
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/app_error.log
CustomLog /var/log/apache2/app_access.log combined
</VirtualHost>
the deployment is on a cloud server using docker image and locally it's working in any way I am accessing the website.
any ideas or setting it might help?
thanks in advance

It uses a relative path, so if your "base"-path is /test1, a relative path would look in a folder called /test1/css/ for the css file.
Depending on your symfony version, the asset() function allows an absolute config parameter to make the path absolute, therefore: what happens if you include the static files like this:
<link rel="stylesheet" href="{{ asset('css/main.css', absolute=true) }}" />
if you're on a version later than 3.0, use:
<link rel="stylesheet" href="{{ absolute_url(asset('css/main.css')) }}" />

i just want to thanks every one tried to help me with my problem but now i found the problem and the solution:
the problem has nothing to do with symfony it self but it was related to the hosting service its seems to be that the service provider is using ingress and Kubernetes and ingress does not support static files (css, js) loading. there is work arounds if you have access to the server settings files but in my case there was not.
my solution was to serve the static files as a service. its somthing like private CDN for my assets and all the static files.
thanks again and if any one had the same case i will be more than happy to help.

Related

Can't access file with RewriteRule in .htaccess

I have a folder Storage/static/css. And I want for design/css to see Storage/static/css content.
/Classes
...
...
/Storage
/static
/css
/home
design.css
/menu
/footer
/js
...
/uploads
...
...
index.php
I tried this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^design/css/(.*)$ /Storage/static/css/$1
RewriteRule ^ index.php [L]
</IfModule>
For index.php works fine for a long time ago.
But for the other one doesn't work at all.
I want for example:
For design/css/home/design.css to return content from Storage/static/css/home/design.css.
Edit:
My entire web app isn't in root. It is inside a folder called /arshwell.
Looks like you accidentally re-rewrite the css requests in a second go...
Have a try with this slightly modified version:
RewriteEngine On
RewriteRule ^/?design/css/(.*)$ /Storage/static/css/$1 [END]
RewriteRule ^ /index.php [END]
This will prevent that requests rewritten to /Storage/static/css/$1 will immediately get rewritten again to index.php by the next rule.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule set will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and in case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Wordpress REST API (wp-api) 404 Error: Cannot access the WordPress REST API

I have been using the Wordpress REST plugin WP-API for months now while developing locally with XAMPP. I recently migrated my site to an EC2 instance and everything is working fine except I now get a 404 with the following message whenever I try to access any endpoint on the API:
The requested URL /wordpress/wp-json/ was not found on this server
Pretty permalinks are enabled with the following structure http://.../wordpress/sample-post/ which works fine when navigating to a specific post in the browser.
Here are some details about my setup:
Wordpress 4.4.1
Not a Multisite
WP REST API plugin 2.0-beta9
Apache 2.2.22
Ubuntu 12.04.5
Any help would be greatly appreciated as I have gone through SO and the WP Support forums for several hours and am out of ideas. Thank you!
UPDATED NEW WAY
I also faced similar problem in a local project. I used index.php after my project url and it worked.
http://localhost/myproject/index.php/wp-json/wp/v2/posts
If it displays a 404 error then update permalinks first (see "Paged Navigation Doesn't Work" section
If it works, maybe you need to enable mod_rewrite, on ubuntu:
a2enmod rewrite
sudo service apache2 restart
Installation
The REST API is included in WordPress 4.7! Plugins are no longer required, just install the latest version of WordPress and you're ready to go.
If you're before 4.7:
Download plugin from here: http://v2.wp-api.org/
install and activate it.
Usage
To get all posts:
www.mysite.com/wp-json/wp/v2/posts
For the search functionality, searching for test post looks like this:
/wp-json/wp/v2/posts?filter[s]=test
I had this problem with the latest WordPress 4.7+. In my case the REST API only worked after I changed the permalinks setting to something other than "Plain", which was the default setting for my installation.
On WPEngine and WP 4.9.2 I only had to update permalinks to get fresh, newly installed site to return v2 API calls. What I did:
Create site
Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
get 404
Go to admin, settings, permalinks, choose "Post Name"
Click "Save Changes"
Browse to http://yoursitename.wpengine.com/wp-json/wp/v2/posts
success. page displays JSON response
It turned out to be a problem with the Apache configuration.
First, I deleted the .htaccess file in the root wordpress directory.
Next, I navigated to /etc/apache2/sites-enabled and opened 000-default
All of the AllowOverride variables were set to None, which I replaced with All.
That did the trick!
It is the file permission error, apply the following solution:
Edit this file /etc/apache2/apache2.conf
Change /var/www/ Permissions from "None" to "All"
Restart the apache2 server.
First you've to Check if the WordPress REST API is enabled or not
The best way to check is to visit this URL: https://yoursite.com/wp-json.
If you see some JSON response, REST API is enabled.
If it’s showing some error page or returns to home page, REST API is not enabled. Then we've to enable it first.
In this case, you've to Enable Permalinks
Visit your page's admin page (Visit https://yoursite.com/wp-json)
settings > Permalinks (visit https://yoursite.com/wp-admin/options-permalink.php)
Make sure Plain is not selected
Choose Post Name (it's a best one)
Then click on Save Changes. That will rewrite/repair your .htaccess
Please see the helping screenshots below:
Source
I solved this issue through following steps:
Navigate to ..\Apache24\conf\httpd.conf and search for LoadModule rewrite_module modules/mod_rewrite.so.
Enable rewrite module by removing the # mark.
Replace all the cases of AllowOverride None to AllowOverride All.
Don't forget to restart apache server. :)
Adding "AllowOverride All" (as hinted by other authors before) to my apache virtual host configuration on my Ubuntu server via SSH did the trick for me:
sudo vi /etc/apache2/sites-available/my-website-name.com.conf
and also (if you use letsencrypt):
sudo vi /etc/apache2/sites-available/my-website-name.com-le-ssl.conf
The files should then look like:
<VirtualHost *:80>
# or <VirtualHost *:443> for the SSL configuration
# [...]
DocumentRoot /var/www/my-website-name.com/public_html
<Directory "/var/www/my-website-name.com/public_html">
# this allows .htaccess files (e.g. generated by Wordpress)
# to overwrite the apache configuration on a directory basis:
AllowOverride All
</Directory>
# [...]
</VirtualHost>
Don't forget to disable and re-enable the site and reload apache to apply the new configuration:
sudo a2dissite my-website-name.com.conf
sudo a2dissite my-website-name.com-le-ssl.conf
sudo a2ensite my-website-name.com.conf
sudo a2ensite my-website-name.com-le-ssl.conf
sudo service apache2 reload
I had to manually make a .htaccess, set it to chmod 664, and copy the permalink rules into it.
I also played around with
Settings > Permalinks
Manually updating .htaccess via the code at the bottom of the permalinks page after clicked "Save"
Adding "index.php" as one of the other answers suggests
Making sure mod rewrite was enabled via a2enmod
I found that mysite/wp-json/ was not working, but mysite/?rest_route=/ was normal. This was breaking some, but not all, the REST API features used on my site.
The answer to this turned out to be a recent change to how I was running my server. This had broken REST API but this was not apparent until later.
I had changed this domain from using Apache to using nginx, and I had not correctly transferred the .htaccess customisations. The quick solution to this problem was therefore to change back to using Apache. This restored the site to working order immediately.
I will be changing this domain back to nginx in the future but when I do, I will test it and be careful not to affect the REST API.
I had moved the WordPress install from a subdirectory to another, so in my case the problem was due to the WordPress config in the .htaccess files. It was trying to redirect every page but the homepage to the old directory. It was just a matter of updating olddir to newdir...
This tripped me up more than once so I thought I'd put it here...
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /olddir/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /olddir/index.php [L]
</IfModule>
# END WordPress
I faced the same problem when I migrated my site from cPanel to Google Cloud Compute Engine Instance; problem was of file permissions which was initially caused due to difference in PHP versions of current deployment from previous deployment.
Here is the fix How to deal with GCP WordPress error "This page isn’t working example.com is currently unable to handle this request. HTTP ERROR 500
If you website is https://example.wordpress.com (on wordpress) for example, use the below link which will give you the JSON response irrespective of any API settings/Permalinks etc..
https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.com/posts
If you want to see all the available endpoints use this -
https://developer.wordpress.com/docs/api/console/
Remember to replace **$site** with your domain
Find the latest documentation here -
https://developer.wordpress.com/docs/api/
If you are on Windows OS change the Permalink Settings as follows:
Custom structure: /index.php/%postname%/
I was facing same issue on localhost and I solved this issue with just set RewriteBase Path in .htaccess file which is available at root folder of WordPress project setup.
**Example:**
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /[folder-name]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /[folder-name]/index.php [L]
</IfModule>
If you have tried the other solutions on this page and they haven't worked, I had success with checking for additional .htaccess files in the root folder above your public_html folder (or wherever you have installed WordPress).
I found an extra one which may have come from a previous installation or been accidentally moved there - it was giving conflicting instructions to the 'real' .htaccess file. Deleting it fixed the problem for me.
For me on new website I didn't had Nginx pretty links setting enabled I add this tolocation / block
try_files $uri $uri/ /index.php?$args;
Try to remove any
order deny,allow
deny from all
allow from ...
from you .htaccess.
I read through a whole bunch of articles and eventually discovered there was a force site to be https plugin installed and activated on my local, however there is no certificate set up. It was making the requests for https when I should have had the https plugin deactivated.
Once deactivated, I was able to make connections to REST API.
Not a very in depth answer but it is was my answer. Hope this saves someone some time!
On WordPress 6
You can use this URL and no need for URL Rewrite and no need to change your Permalink settings (I saw Gutenberg editor is using this URL to access REST)
http://example.com/index.php?rest_route=/wp/v2/posts

Where to place css file for perl cgi script?

I am currently using Apache Server to run my script with the URL:
http://MY_IP/cgi-bin/example.cgi/
which works fine.
However, I'm not sure where to put my .css file. I read that it doesn't belong in the cgi-bin directory.
This is what I have in my 000-default.conf file.
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
I have tried placing it in here and used:
<link rel="stylesheet" type="text/css" href="/var/www/html/login.css"/>
but to no avail.
Could someone please help me out with this?
Thank you.
Looks like you put it in the right place. It's just the href that you've got wrong.
Your Apache config contains this line
DocumentRoot /var/www/html
This means the the root of your web site is at /var/www/html. So a URL that looks at the root of your web site will translate to a file in that directory.
The href attribute is a URL, not a file path. Therefore, if you put your login.css in this directory, then the correct link would be:
<link rel="stylesheet" type="text/css" href="/login.css" />
But many people would put their CSS files in a subdirectory called /css/ (which maps to /var/www/html/css on your filesystem). If you do that, then the correct link becomes.
<link rel="stylesheet" type="text/css" href="/css/login.css" />
Although it is always a matter of personal preference, a lot of people create directories under their root directory for javascript, css, images, etc. -- so any static pages would be in /var/www/html, and assets are in subdirectories, such as /var/www/html/css, /var/www/html/javascript, /var/www/html/images, and so on.
In your static html pages (in /var/www/html), you can reference these using relative links, e.g. <link rel="stylesheet" href="css/style.css">, <script src="javascript/my-script.js"> or absolute links, e.g. <link rel="stylesheet" href="http://MY_IP/css/style.css">. For pages on your server but not in /var/www/html, using /css/style.css is the appropriate form of the relative link, as / is equivalent to the local directory /var/www/html (that is what setting DocumentRoot in Apache does), so /css accesses the local directory /var/www/html/css.
You can also use the <base> tag in the page head to set a base href for all your relative links:
<base href="http://MY_IP">
Then any relative links (e.g. href="css/style.css") will automatically be interpreted as (e.g.) http://MY_IP/css/style.css.

Deploy symfony2 into a folder

As a symfony1.4 developer, I'm trying to learn Symfony2.
Now, instead of trying on my local machine, where all works fine, today I want to make my test site available on my server. But I don't want to deploy it on a real site or on a real subdomain.
And that's my question: how can I deploy this test on a subfolder without modify virtualhost?!
Lets say I have an available site, www.example.com, but I'd like to have my symfony test available (and working correctly) here www.exaple.com/mysymfonytest
Did somone already do that?
Thanks...
In my opinion that is not really a symfony related question.
Depending on your webserver you could configure an alias where which url should point.
In Apache you specify your document root, where you put your sf2 installation, and then define an alias which would be /mysymfonytest pointing to that document root.
Alternatively you could specify your document root, leave the url be and define a prefix for all routes in symfony, which you would do in your routing configuration.
I strongly recommend configuring your webserver though.
For example in apache:
<VirtualHost *:80>
DocumentRoot /var/www/mysymfonytest
Alias /mysymfonytest /var/www/mysymfonytest
<Directory /var/www/mysymfonytest/web>
AllowOverride None
Order allow,deny
Allow from all
#Allow from <SUPPORT>
RewriteEngine On
RewriteBase /mysymfonytest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</Directory>
</VirtualHost>
Alternatively, if you cannot add the VHost:
How to add a prefix to all my routes
make a sub-directory under / (root directory) and call it mysymfonytest then put your symfony2 project in mysymfonytest.
add a .htaccess file to mysymfonytest folder and put in it :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ web [L]
</IfModule>
or you can just make a file named index.html and make it point to app.php
If your only running on a shared hosting. You could use override configurations using .htaccess. Ask your provide if they allow overrides and then provide them the script or tell them what you want. Hope that's covered on your hosting support.

Have WordPress with a different index file for testing

A site I'm designing a website for was built with static HTML files. I'm converting it to WordPress but I need the old site to still function while WordPress is installed in the same directory (e.g. have index.html be the default one and then have WordPress be something like indexNew.php)
So they go to http://domain.com/indexNew.php to see updates to the new website until it's finished.
How would I configure that in the .htaccess file?
You can use a .htaccess with a simply rule to redirect users to index.html except you :
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.html$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteRule $ /index.html [R=302,L]
Replace 123.123.123.123 by your public ip adress.
You should use your .htaccess to set the default document order:
Tired of having yoursite.com/index.html come up when you go to yoursite.com? Want to change it to be yoursite.com/ILikePizzaSteve.html that comes up instead? No problem!
http://www.javascriptkit.com/howto/htaccess6.shtml
In your case, you won't need to rename the index.php at all - this will do the trick:
DirectoryIndex index.html index.php
Anyone who requests yoursite.com will see the content from index.html. To see the new Wordpress content, just navigate to yoursite.com/index.php

Resources