I'm using the following setting for url rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In index.php is parse $_GET['url'] so that in the following examples:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
So that the first parameter(? i don't know how to call it) is the page name then the following couple of parameters are like "keys/value".
Now when i browse ROOT/ the link to stylesheets that are inserted inside the html of the page and the page are shown correctly.
I fi browse ROOT/index (which is the same as ROOT/) it shows the page (with contents and other stuff) correctly but the links (even if in the html structure are correctly written) to stylesheets are not loaded. And i can see that from the fact that my page has no css at all when i load it.
How can I fix this?
EDIT
The css file's path is as follows:
project/view/css/common.css
The file where is it included is in
project/public/index.php // the one with .htaccess and rewrite rules
This brings me to make a link (inside the index.php) such as
../view/css/common.css
But this works different depending on how the url seems. For examples:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
Comment doesn't let me format the code, can you please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Update
You can try something like this in the <head> section of your pages to support relative path for your image/css/js files referenced on that page:
<head>
<base href="http://www.example.com/static-files/" />
</head>
I think you have two problems (as the given answers didnĀ“t solve your problem yet...):
You are rewriting all file-names so when the browser is requesting css/index.css your rewrite transforms that to index.php?url=css/index.css. #cOle2's answer should solve that.
You are not using absolute paths for your css files. The server is translating the requested page like /user/id/1/name/bobby to /index.php?etc.... but when the browser requests for example the css file that is something like css/index.css in your code, it will actually request /user/id/1/name/bobby/css/index.css and that file does not exist. You can solve that by using only absolute paths to your external files (css, js, images, etc.) like /css/index.css.
Edit: Based on your comments, both your paths are relative and your css is not accessible by the browser, so you need to:
Move the css to the project/public directory (like project/public/css)
Use absolute paths to your css like /css/index.css.
Make sure the urls to your external files are not rewritten by the server.
You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.
# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
Edit: This is how I would apply it:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Related
I have a url like this:
mysite.com/1/everything
and I want to rewrite it and only get the number in the url (e.g. 1 above). For that, I have written a htaccess like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule for root pages
RewriteRule (^\d+\/?([\x00-\x7F]|[^\x00-\x7F])+\/?$) index-continue.php?id=$1 [NC,L,QSA]
But all of my css files and image files and js files get rewritten! How can I solve this problem? This causes all the file rewrites on the wrong path.
Your .htaccess file should only keep the following thing, it will allow your js and css to open directly with the path root/css/file.css and root/js/file.js
RewriteEngine On
RewriteRule ^style/([^/.]+)?$ style/$1
RewriteRule ^js/([^/.]+)?$ js/$1
RewriteRule ^([^/.]+)/([^/.]+)?$ index-continue.php?id=$1&%{QUERY_STRING}
To fix the css and js path on rewritten urls, you need to add the following base tag to the head section of your webpage :
<base href="/">
Related :
Seo Friendly Url css img js not working
Here is my problem
in my website base url am redirecting to an inside folder
eg: http://example.com/
to
http://onlinevyapari.com/business/ by using this code in .htaccess file
RewriteRule ^(/)?$ /business/index.php [L]
now I want to use search engine friendly url for the same website my query is like this
http://example.com/business-details.php?id=106
but I want to keep my url like this
http://example.com/business-details/106
I have done in different way like bellow
RewriteEngine on
RewriteRule business-details/id/(.*)/ business-details.php?id=$1
RewriteRule business-details/id/(.*) business-details.php?id=$1
its happening but css is not loading properly
it will be really appreciable to me if somebody help.
thank you
Have your rule like this:
RewriteEngine on
RewriteRule ^(/)?$ business/index.php [L]
RewriteRule ^business-details/(\d+)/?$ business-details.php?id=$1 [L,QSA,NC]
For solving css/js/image path issues just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
You need to add the following, before your rules, to make the rewrite rules ignore file and directory names.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
In my .htaccess file I rewrite all URLs to my index file like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?string=$1 [L,QSA]
So far, so good. When I request the url example.com/path/to/my/page the value of $_GET['string'] is path/to/my/page, as it should be.
Now the problem:
in my index.php I use several script/css files like this:
<script src="script/script.js"></script>
With these rewrite rules on, the script/css files get requested as example.com/path/to/my/script/script.js.
Is there a way to make sure the script gets requested without the /path/to/my part?
The only solution I found is putting the entire url in the src attribute, but I don't like it that way.
Note: the path part can consist of a variable number of parts, so /long/path/to/another/page or /page are also possible URLs.
You can also add a relative URL base to the header of your index.php file:
<base href="/" />
Hi I am using the following lines in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /coaster/CoasterInsider/
RewriteRule signup$ index.php?page=signup [L,NC]
RewriteRule login$ index.php?page=loginHandle [L,NC]
RewriteRule u/(.*)$ index.php?page=profile&username=$1 [L,NC]
The path is getting redirected properly, but the css,flash and images are not loading. Also if I use the following '/' after any url say,
RewriteRule signup(/?)$ index.php?page=signup [L,NC]
It's not finding any page there, i.e. error 404. I just want my htaccess file to work for both /signup and /signup/
When browser displays:
http://example.com/coaster/CoasterInsider/signup
http://example.com/coaster/CoasterInsider/signup/
and encounters a relative URL such as:
<img src="site/images/photo.png">
It translates the relative URL to (respectively):
http://example.com/coaster/CoasterInsider/site/images/photo.png
http://example.com/coaster/CoasterInsider/signup/site/images/photo.png
You should use absolute URLs for assets. Make this a habit:
<img src="/site/images/photo.png">
Alternately you can use the HTML base tag in your pages which tells browsers how to treat relative URLs. Personally I do not recommend it.
This was advised to, and worked for me, place it inside the <head> tags in your html: <base href="/"> Again, it worked for me, I hope it helps someone else.
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to exclude real files and directories from being rewritten.
You could also add
RewriteCond %{REQUEST_FILENAME} !-l
to do the same for symlinks.
basically what im trying to do it :
http://mydomain.com/1/1
to
http://mydomain.com/index.php?mid=1&mpid=1
and im using these codes in htaccess.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
it works fine but the problem is that css get messed,its not loading css and images.
whats the solution ?
The problem is that your CSS is no longer relative to the root of your site.
Your index.php file is at /, but when you rewrite to /1/1, the client thinks you actually are at /1/1, and looks for the CSS file relative to that path, as it should.
What you need to do is reference your CSS at the root, /style/something.css or whatever it is. Just make sure you have / at the front of that path.
Use the base element to set the base URL for all assets (CSS and images, etc). Here's the docs: https://developer.mozilla.org/en/HTML/Element/base
<base href="http://www.yourdomain.com/">
Also, your .htaccess may be redirecting the calls for your CSS and images. Change your .htaccess to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
... that will check to make sure the requested file (-f) and the requested directory (-d) don't exist before doing any redirects.
There can be two problems:
CSS and Images are not loading at all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
Or the images now have different path. Always put slash at the beginning of the path, it will help. Especially in CSS file, where the path is taken relative from CSS file, not actual url.
Prepend
RewriteCond %{REQUEST_FILENAME} !-f # Existing File
before your RewriteRule - it prevents existing files, like css files, from being rewritten.
Maybe the sript sends the wrong Content-Type. CSS-Data requires Content-Type: text/css.
PHP:
header("Content-Type: text/css");
With a file extension like '.php' no browser will sniff it correct.