Url friendly without losing styles css / js / images - php

I have a website too extensive editing the URLs of styles css and files js, images would cost me too much time.
I'm working at localhost
localhost/project/index.php
The friendly url I want to have:
localhost/project/index/
From this url
localhost/project/online.php
to this
localhost/project/online/video/hd/free/
I have observed this website http://crazycafe.net/demos/seo/ and its source code, the styles css is maintained this way: <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
without adding an absolute URL to styles css and their files and images.
How do i create a friendly url this way?
If the user modifies the url in this way:
http://crazycafe.net/demos/seo
Redirect to this way:
http://crazycafe.net/demos/seo/
On the other hand like conserving the styles css the files js and images, without having to modify to an absolute route.
My file directory is:
assets/css/style.css
assets/js/app.js
assets/fonts/icons/image.png
assets/fonts/ttf/roboto.ttf
assets/img/system/image.png
assets/img/logo.png
Note:
I found this question in SO, of how conserve CSS styles.
But I do not understand the use of friendly URL - .httaccess

You need to switch on mod_rewrite and use it. Example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/ [R=301,L]
Folder-based example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]
Your case:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online.php$ project/online/video/hd/free/$1 [R=301,L]
See more here.

Related

CSS not loading after URL rewriting

I am trying to rewrite URL from example.com/test.php?id=hd3j3 to example.com/id/hd3j3.
The problem is rewriting occurs and I am taken to the page but the css and js of the page doesn't load. Where am I going wrong?
.htaccess
RewriteBase /
RewriteEngine On
RewriteRule ^id/([A-Za-z0-9]+)$ test.php?id=$1 [L]
Your rewrite rule looks right for me.
Maybe your css gets loaded from a relative path and not absolute ?
<link href="css/layout.css">
The browser try to load from example.com/id/css/layout.css instead of example.com/css/layout.css
Try to clear the cache and retry ! This usually works.Or set the root path correctly.
If these won't work try this,
If you want to use image tag and show to correctly images, use
" />

After using .htaccess RewriteRule page not opening properly

I am trying to convert my dynamic PHP link to a clear link; for that I used RewriteRule. But the work seems to be not done properly
I am trying to convert
dappersole.in/product.php?product_id=45;
To
dappersole.in/product/46;
Using
RewriteRule ^product/([0-9]+)$ product.php?prodct_id=$1 [NC,L]
After this the page does not load properly. But if I remove product/ from RewriteRule, the page loads perfectly
RewriteRule ^([0-9]+)$ product.php?prodct_id=$1 [NC,L]
dappersole.in/45;
Your page won't load fully because you are referring to your CSS and JavaScript resources relative to the current location. You need to change this to refer to the resources relative to the root of the site. For example, change this:
<link rel="stylesheet" type="text/css" href="css/slider-pro.min.css" media="screen"/>
to this:
<link rel="stylesheet" type="text/css" href="/css/slider-pro.min.css" media="screen"/>
Note the leading slash before css/slider.... You need to do this for all your CSS, jQuery and image resources. Alternatively, you can save time by adding the following to your <head> node:
<base href="/">
Or, even better:
<base href="http://dappersole.in/">
Additionally, I recommend you do the same with your mod_rewrite rule:
RewriteRule ^product/([0-9]+)$ /product.php?product_id=$1 [NC,L]
Lastly, I recommend you ensure Multiviews is not enabled, by adding this line to the top of the file:
Options -Multiviews
I too have had similar issues to this with Apache 2.2 - if the name and the rewritten name are very similar it fails. You could cheat slightly and use a different url such as http://www.example.com/item/46
RewriteRule ^item/([0-9]+)$ product.php?product_id=$1 [NC,L]
I've had that same problem on an older version of apache / mod_rewrite and I never found out what the exact problem was. But it definitely did not like rewriting /something/123 to /something.php?....
The quick fix I used, was renaming my php file to _something.php and using that.
So in your case renaming product.php to something that does not start with product like _product.php:
RewriteRule ^product/([0-9]+)$ _product.php?product_id=$1 [NC,L]
^ strangely enough this solved my problem...

mod_rewrite RewriteRule and html links

I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?
Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>
Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [L]

.htacces Appears only html with no CSS or PHP

I've been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/
Well, I inserted my url(working on localhost):empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia
and it gave me this(with all options by default):http://empresa.com/sub_artigo/5/Mercearia.html
And the .htacces code was this:RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]
And when I generate the url in php I do
$response2[nome_sub_artigo]
And then, when I click the button, it appears like, only html.
example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg
Any idea what is happening ?
It looks like you are using relative links for your assets (images, javascript, css).
That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.
The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.
Your PHP code is outputting relative paths to the style sheets.
Example:
<link rel="stylesheet" type="text/css" href="styles.css">
Given your example input URL, this causes the browser to look for a stylesheet at the following URL:
http://empresa.com/sub_artigo/5/styles.css
This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.
Instead you should use an absolute path, such as:
<link rel="stylesheet" type="text/css" href="/styles.css">
Notice the leading / on the path? This will tell the browser to look from the root of the domain:
http://empresa.com/styles.css
In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.
Try this code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]

htaccess url rewrite many links and protect css and js files

I want to rewrite my links but with multi styles
here is i want i want to be look like
Info cms is my Rewrite Base
Cats style
http://www.site.com/catname
Post Style
http://www.site.com/anycatname/posttitle
Pages Style
http://www.site.com/page/page+title+here
My Pages is
cats style => cats.php?name=catname
posts style => posts.php?title=posttitle
page style => page.php?name=catname
Note
As I tried to make url rewrite as in posts style css and js files had many problem too
See below url
http://corz.org/serv/tricks/htaccess2.php
or try below
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]
would allow you to present this link as..
mysite/files/games/hoopy.zip
and in the background have that transparently translated, server-side, to..
mysite/download.php?section=games&file=hoopy

Categories