directory problems when using htaccess - .htaccess

I've figured out how Mod rewrite works now however I've come across a new problem.
htaccess code
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/$ http://localhost/testblog/index.php?category=$1
I now know that this: localhost/testblog/dogs/
is equal to : localhost/testblog/index.php?category=dogs
however, when i put a link on the index.php file; Dogs that page (localhost/testblog/dogs/) now has the stylesheet prefixed with dogs/ so the stylesheet looks like localhost/testblog/dogs/stylesheets/style.css
but when I link with Dogs everything works just fine; localhost/testblog/stylesheets/style.css
I find it confusing because I thought that they now equaled the same thing using the .htaccess file?
how can i rectify this problem with the prefix?
Hopefully I've explained properly
Thanks

I think your issue is probably more the way you're linking the stylesheet. You're re-writing to /dogs/ and I'm guessing your stylesheet is linked like <link href="stylesheets/style.css" rel="stylesheet" type="text/css">. So when you point your browser at /dogs/ it's trying to load a stylesheet in relation to the URL, and it looks like /dogs/ is a folder so it's just appending stylesheets/style.css to your current URL. The best bet in my opinion would be to link absolutely to the stylesheet like: <link href="/testblog/stylesheets/style.css" rel="stylesheet" type="text/css">

Like said Jonathon, the browser compute relative link by appending the href to the url displayed on the adress bar.
To change this, you need to had a base tag
<base href="//localhost/testblog">
So, your browser will compute your link by adding your href to the base href.

Related

Canonical URL Issue

I am experiencing an issue with setting canonical urls for my website. Moz crawler shows that each page is missing a canonical and when I inspect the page, the canonical is shown as follow:
<link rel=""canonical"" href=""https://website.com/"">
This goes for each webpage.
The website was built by a third party and the wait time for corrections is rather long. I've checked the CMS and the repository for errors, but nothing looks out of the ordinary. Any ideas?
edit: I should mention that I am aware of the extra quotes, but am unable to identify the error. Repository lists the following:
<link rel="canonical" href="{site_url}/{segment_1}/{segment_2}" />
To be valid it should look like so:
<link rel="canonical" href="https://website.com" />
It might be that the segments aren't set properly. Maybe they are set with something like freebie?
Anyway, the way it's set up now you will get some unwanted slashes if there are no url segments.. Maybe better to change this to something like <link rel="canonical" href="{site_url}/{if segment_1}{segment_1}/{/if}{if segment_2}{segment_2}/{/if}" />

.htaccess setting for local pathing and development

My initial feeling is this can be done with .htaccess, but if there is another/better way I'm open to it.
I'll try and explain what I'm doing and want.
I am developing a site (newsite.com), to replace currentsite.com.
There is already a site up an currentsite.com domain.
I figured I'll try and develop on my personal domain, until tested ready to launch, and move things over, replacing current site.com
So I have personalsite.com (my personal domain)..and I created a directory called staging.newsite.com
So the full URL is http://www.personalsite.com/staging.newsite.com.
However, I am having trouble pathing to CSS and JS files with this set up.
In the header file I load my css as so:
<link href="_includes/css/default.css" type="text/css" rel="stylesheet" />
Doing this:
<link href="/_includes/css/default.css" type="text/css" rel="stylesheet" />
makes it point to _root of www.personalsite.com to look for includes folder.
which seems to work fine when I go:
http://www.personalsite.com/staging.newsite.com
but when I go to a content page on the staging.newsite.com, like:
(for example)
http://www.personalsite.com/staging.newsite.com/sub-dir1/sub-dir2/contentpage.php
The CSS breaks..
Is there a way to set this up so http://www.personalsite.com/staging.newsite.com is seen as the root directory for this staging.newsite.com? Or some other way to set it up for easier developing and then will be a simple upload to its final domain (without the need for the .htaccess rules?)
I tried this:
RewriteCond %{HTTP_HOST} !^staging\.newsite\.com$ [NC]
RewriteRule (.*) http://staging.newsite.com/$1 [R=301,L]
but that re-directs the MAIN personalsite.com to http://www.staging.newsite.com (which of course is just a subdirectory of personal.com for development purposes..etc)
update: 08.11.2015
edit: This is NOT a vanity URL re-write question.. please treat it as such, and do not post it as a duplicate of that topic. If you feel it IS the same, explain why and how the same solution applies, out of context links do nothing for anyone.
update: 08.11.2015
I believe what I am looking for/to do is referred to as: "normalizing" the sub-domain/directory? (new term for me, so I'll search on that as well)

.htaccess to redirect images

i'm still new to the redirectrule thing from htaccess and have successfully redirected my pages so that the pretty URL shows up, but is there a way to redirect the images so that it is not relative? Is there a way or function that allows me to redirect images without changing it one by one ?
Like i after redirecting the page, i get relative URL, hence my images are showing something extra in their links.
so this is the actual url to the image
http://www.yousite.com/page/images/this.jpg
but i'm getting stuff like
http://www.yoursite.com/images/page/images/this.jpg
I am not sure this will solve the problem, but instead of modifying all the links you could use the BASE element in each page to define a base URI:
From http://www.w3.org/TR/html4/struct/links.html#h-12.4:
When present, the BASE element must appear in the HEAD section of an
HTML document, before any element that refers to an external source. The
path information specified by the BASE element only affects URIs in the
document where the element appears.
Examples:
<HEAD>
<BASE href="ImagesBasePath">
</HEAD>
Or just:
<HEAD>
<BASE href="/">
</HEAD>

Convert to SEO friendly URL

I'm trying to make my website URLs SEO-friendly, and I'm having problems...
How can I change this URL:
http://www.example.com/media.php?album=Album_1&page=1
To this kind of URL:
http://www.example.com/media/Album_1/1
I've tried this code that works, but my CSS seems KO...
RewriteEngine On
RewriteRule ^media/(.*?)/([0-9]+)$ media.php?album=$1&page=$2
Your rule's target doesn't need the &, it should just be &. Apache will propery encode it as needed (like in the anchor link in the HTML returned in a redirect). The reason your css isn't working sounds like a relative vs absolute URL issue. If your css is linked like <link rel=stylesheet href="something.css">, the relative URL resolution ends up being /media/Album_1/something.css.
Try adding this in the headers of your pages:
<base href="/">
Try reading mod_rewrite apache documents or just searching 'mod_rewrite' This specific question was asked many times

Htaccess Rewriterule virtual directories breaking all links

I think I've gone through a million articles and everyone's codes on this, but I just can't get it to work! Every code I try works but it shows the redirect url instead of the virtual one.
I would like mysite.com/company/client/ to actually show the results from mysite.com/company/client.php, but the files within client.php become broken unless I make absolute links.
For instance mysite.com/company/css/style.css is broken when viewing from http://mysite/company/client/, but is working fine when going to the real file mysite.com/company/client.php
I hope someone knows how to solve this problem without having to change all links in client.php to absolute!
Thanks!
Scott
You might want to consider the HTML BASE tag:
...
<head>
...
<base href="http://mysite/company" />
...
</head>
...
You put this in the client.php file, then any relative link & reference is automatically "converted" to an absolute one, starting from the right folder.
See the comments on possible issues with the BASE tag.
Another option would be to perform a redirect instead of a rewrite, by adding "[R]" to the RewriteRule. However, then your users will see the "client.php" in the URL.

Resources