Reputation: 638
I have the following file structure
|---api
|---css
|---img
|---js
|index.php
|rewrite.php
|book.php
What I basically do is, redirect every single request to rewrite.php and then based on first parameter of request URI, include
the corresponding page through PHP.
My rewrite.php file:
$path = rtrim($_SERVER['REQUEST_URI'], "/");
$elements = explode('/', $path);
if($elements[1] == "about-us") //Something like www.domain.com/about-us
{
require_once("about-us.php");
}
For booking, I check as following: (Something like www.domain.com/city/service)
if($elements[1] == "somecityname" && $elements[2] != "")
{
$_GET["serviceID"] = Service ID of service retrieved through DB;
require_once("book.php");
}
But book.php file includes the CSS and JS files as www.domain.com/city/css
and www.domain.com/city/js
and hence comes up with the error 404.
There is some problem with my HTACCESS. My current file is:
RewriteEngine on
RewriteCond %{THE_REQUEST} /(pg|api|css|js|lib|customerapi|agentapi)[/\s] [NC]
RewriteRule ^ - [L]
#rewrite to https and www
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www|\d+)\.
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
#rewrite url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /rewrite.php [L,QSA]
Please help me where am I going wrong.
Upvotes: 1
Views: 92
Reputation: 785376
But book.php file includes the CSS and JS files as www.domain.com/city/css and www.domain.com/city/js and hence comes up with the error 404.
That I believe is due to your use of relative paths in including css
and js
files.
For that you can add this in the <head>
section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
EDIT: To exclude certain folders have this rule just below RewriteEngine
line:
RewriteCond %{THE_REQUEST} /(pg|css|js|exclude1|exclude2)[/\s] [NC]
RewriteRule ^ - [L]
Upvotes: 2