Fix SRC Urls by HTACCESS

Asked

Viewed 259 times

0

I am using URL friendly via HTACCESS and because I am using it the pages lose the Styles and js. Because the path (URL) is done as ex /category/pizza so it looks for src="category/pizza/style.css" . Would you have some way to fix the base URL by HTACCESS?

  • We await a feedback on the answers already given

3 answers

0

In this case the ideal is for you to specify the path of the files to a) full URL or b) relative path (e.g., "/style.css" or "/css/style.css"). This problem is not related to htaccess

  • There would be no way to enter the URL from HTACCESS?

  • If you have a limited number of files, it would be possible however you lose flexibility with it. The recommended way is to adjust to find the file in the correct path.

0

An alternative is to set the tag <base> in the header of HTML pages.

http://www.w3schools.com/tags/tag_base.asp

Also set in httaccess the rewrite rule exception for when to find a folder or file, such as the basis where the rule will be applied.

RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

0


My recommendation is that you always specify the absolute path of the file, since PHP interprets based on the server path and the src and href in relation to the browser path... in such cases you would have to create a variable or a constant to resolve this difference

$UrlSite = explode(DIRECTORY_SEPARATOR,trim(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH), '/\\'));
$DirSite = explode(DIRECTORY_SEPARATOR,__DIR__);
if(in_array($UrlSite[0], $DirSite) AND $UrlSite[0] != ''){
   $Directory = "/$UrlSite[0]/";
   //é atribuido ao smarty {$Directory} no arquivo de configuração do smarty
}else{
   $Directory = "/";
}

In the variable above I compare the path of the server with the path of the browser, if the folder is the same in the 2, it saves in the variable... then we src and href I put the variable always in front of the absolute path. Ex:

<a href="{$Directory}images/press/large/image.jpg">

And here the folder imagens is at the root of my project

Now something I consider very important, why not use the tag <base>? Simple, because it is not compatible with old browsers, IE9 or earlier will still not find your files, IE, you would have to stop giving support for these versions...

My htaccess is as follows

<IfModule mod_rewrite.c>
   RewriteEngine On
   Options -Indexes
   ErrorDocument 404 /Errors/404.html
   ErrorDocument 403 /Errors/403.html
   RewriteRule ^/?$ page/php/Home.php
</IfModule>

At first this was the best solution I could find, at least it was the only one compatible with all needs and all browsers... I couldn’t find any way to do it directly by . htaccess

Browser other questions tagged

You are not signed in. Login or sign up in order to post.