.htaccess setting for local pathing and development - .htaccess

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)

Related

Problem of links when create friendly links with htaccess [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

SPA .htaccess breaks JS files after top level [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

directory problems when using 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.

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