I am trying to rewrite my URL but when I do in htaccess, the page load is taking forever and when it loads there is no styling and the images are blank, can't seem to tell what the problem is, I don't see any problem in the code, can you help, please ?
Rewrite clean url for profile.php?user_id=1§ion=discussions
RewriteRule ^profile/([0-9]+)/([0-9a-zA-Z_-]+) profile.php?user_id=$1§ion=$2 [NC,L]
Solution is adding
<base href="/">
in the head tag
Related
I'm trying to clean my URL path's so that I don't have any GET parameters and PHP extensions in all of my links. As an example for what I'm trying to achieve:
http://localhost/projectname/?page=dashboard
needs to be:
http://localhost/projectname/dashboard/
And it actually works, I used the following code in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} dashboard/
RewriteRule dashboard/ http://localhost/projectname/?page=dashboard
However, the page displays itself without any CSS or Javascript. I tried navigating to my Style.css only to find out that it looks exactly like the webpage itself, instead of showing me my CSS rules.
So what am I doing wrong? Please don't mark my question as a duplicate, I've been looking into similar questions but couldn't solve the problem.
You're changing the relative URI when your URL goes from /projectname/ to /projectname/dashboard/.
When that happens, every relative link on the page will have the wrong base added to it. The browser has no idea that the base is actually /projectname/ when all it sees is the location being at /projectname/dashboard/.
Right now, when your css is linked like:
<link rel="stylesheet" href="Content/style.css">
The browser attempts to resolve it by adding the base from the location, and it'll load:
https://localhost/projectname/dashboard/Content/style.css
which doesn't exist because the "dashboard" isn't actually a folder.
You can either add a base to the actual page by including this in the page header:
<base href="/projectname/" />
Or you can try to un-rewrite the dashboard out of the request (a bit trickier and more error prone) by adding this rule
RewriteCond %{REQUEST_URI} ^/projectname/dashboard/(.*\.css)$
RewriteCond %{DOCUMENT_ROOT}/projectname/%1 -f
RewriteCond ^projectname/dashboard/(.*\.css)$ /projectname/$1 [L]
Note that does this may have an unintended impact on your browser's cache.
Use the absolute path to the CSS.
<link rel="stylesheet" href="http://localhost/projectname/assets/style.css">
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
" />
I have a simple redirect rule as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^contact-us\/?.*$ ./contact-us.php [NC,QSA,L]
This rule works fine for the following URL:
http://localhost/folder1/folder2/folder3/contact-us/
The problem is that if I remove the final slash, the page loads properly but the resources (css/js) are not loading
http://localhost/folder1/folder2/folder3/contact-us ----> This fails
It seems it is omitting a folder when trying to load the resources so rather than http://localhost/folder1/folder2/folder3/js/jquery.js, the URL for the JS resource is being set as http://localhost/folder1/folder2/js/jquery.js
Is there a concept missing here with the rules? How to make both URLs work?
This is happening due to your use of relative paths in js/css etc.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/folder1/folder2/folder3/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
I've been struggling with this as I'm not so good in rewriting.
I want a URLs like these:
http://www.example.com/d/page1
http://www.example.com/d/page2
http://www.example.com/d/anythinghere
to always resolve (rewrite) to this:
http://www.example.com/dir.php
or maybe event better to:
http://www.example.com/dir
which in turn should be rewritten to /dir.php
For those who would like to know why is this needed:
I need to have my AngularJS non-single-page-app work without hashbangs where I need my pagination or anything - I want to have distinctive page URLs in order for the Web spiders to crawl my content properly.
So I'm hoping that I will be able, by making such requests resolve always in my page where AngularJS is dir.php to have links: Go to page 3
I'm still not sure if this is going to work at all. Anyway, the purpose of this rewrite thing is to force the server not to go away from this page when such a link is clicked. This just struck me: but it would create at least a page reload, wouldn't it? If so, that's really bad...
RewriteRule ^/d/(.*)$ dir.php/$1
RewriteRule ^/dir/(.*)$ dir.php/$1
First rule will change everything afer /d/ to /dir.php
Second rule will forward everything after /dir/ to /dir.php
on your menu change the link
<a href="dir.php">Your Menu <a/>
to
<a href="dir">Your Menu <a/>
in the. htaccess file try this
RewriteEngine On
RewriteRule ^dir dir.php
Actually, all my previous attempts were valid - the thing is I was getting broken layout so I assumed rewrites weren't completely correct. It turned out including <base href="/"> rectified the thing by forcing the paths to be relative to the root.
Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.