im still new to .htaccess
so i looked on stackoverflow for my answer and i cant find it. i have rules for my site i had to setup so i got my rules from this:
SEO Friendly URL to Dynamic URL using PHP
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [QSA,L]
but for some reason my css is broken but my php is good
example url:
http://www.domain.com/aboutme
http://www.domain.com/css/version.css
The problem is due to Capitalized file name Version.css.
If your devbox is windows it'll work. Windows is case insensitive.
But when you test it in server (Unix) it will break.
Because Unix is case sensitive
Better rename Version.css to version.css. Its safe for the future
I have a super answer for this problem. I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?
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 decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />
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]
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]
I have just one root directory with index.php in it along with two folder img and css.
I refer to files in this folder like:
src="img/path.png" i.e relative to the root directory.
The other day I had some mod_rewrite question & this is what someone gave me, which seems to wrok fine except for trailing slashes and css/img breaking apart
RewriteRule ^$ index.php?page=1 [L]
RewriteRule ^([0-9]+)/?$ index.php?page=$1 [L]
RewriteRule ^([A-Za-z]+)/?$ index.php?category=$1&page=1 [L]
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]
This is what is bothering me:
Using Rule1:
www.example.com changes to www.example.com/index.php?page=1 which is great
Also www.example.com/ some how changes to www.example.com which is again great
Using Rule2:
www.example.com/2 changes to www.example.com/index.php?page=2 like what I would want
But using www.example.com/2/ (TRAILING SLASH) also retrieves page=2 but somehow the img and css breaks apart.
I am guessing the problem is with url being treated as directory structure and then it cant find img and css folder there.
Using Rule 3:
www.example.com/Football changes to www.example.com/index.php?category=Football&page=1 again like what I would want
But www.example.com/Football/ (TRAILING SLASH) suffers from the same problem with img and css breaking apart
Using Rule 4:
www.www.example.com/Football/2 even without the trailing slash breaksdown on css and img however the page can retrieve tha page and category correctly.
How do I correct this problem without having to use absolute paths in my html.
Please advise on the trailing slash problem as well.
Can you not reference your images/css using relative but starting with a /?
/images/blah.jpg
/css/style.css
You could add a rule like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|favicon.ico)
# Other rules here
The idea is to avoid the rewrite case for an existing file or directory.
With a trailing slash, the path will be interpreted as a directory. Wherever you include images, css files and such you have to change the paths. Say you have images in an img folder, you probably use it in css like:
background: url(img/image.png)...
Change that to :
background: url(/img/image.png) ...
Same goes for everywhere you have images referenced or other paths. Using a path that starts with "/" means that you are searching from the root directory. The rules you're using are the rules that I'm ussually using and didn't have any problems with them. Actually, I find it easier to build my paths with a starting "/".
Just as a last example, if you have: mysite.com/home and you have <img src="img/image.png" /> the image will be searched in mysite.com/img/image.png which is probably what you want.
But if you have mysite.com/home/ and <img src="img/image.png" /> the image must reside in mysite.com/home/img/image.png which is not what you want, thus using <img src="/img/image.png" /> would be the solution for you.