Htaccess Rewriterule virtual directories breaking all links - .htaccess

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.

Related

Odd URL RewriteRule Behaviour

Hoping someone with better experience can guide me on this.
Ref: https://www.donbur.co.uk/gb-en/news/index.php
This is a dynamically generated news summary list with a button link to article pages. Until last week, the button links worked fine.
Each link calls a URL
<a class="button" href="https://www.donbur.co.uk/news/<?php echo rawurlencode($row_newsResult['newsurlkey']);?>">Read full article</a>
This parses fine.
To process this link, I've then got an htaccess RewriteRule:
RewriteRule ^news/([^/]*)$ /gb-en/news/article.php?title=$1 [L]
The article.php file then uses the unique title to identify and show the full article.... or at least that's what it should do and has done.
For whatever reason, from the news index.php page, when the link is clicked, it just refreshes the page and re-writes the URL to
https://www.donbur.co.uk/gb-en/news/index.php/[uniquetitleparameter]?title=[uniquetitleparameter]
The parts in brackets [] obviously get populated with the rawurlencoded string.
I cannot for the life of me understand what is happening here - especially when I have a similar page, doing a very similar thing, works absolutely fine (https://www.donbur.co.uk/gb-en/features/feature-list.php)!!!!
This all started going wrong when I converted the site from http to https: but can't work out if that's relevant??
Turns out this was just a bad caching issue. Temporarily disabled cache as suggested and all works well.

.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)

ModX wont work without absolute paths

I have built a site on Revo 2.1.3. All went well except for the site paths. Where I used to link to an image like:
assets/images/photo.jpg
It now wont work (appears as broken link) unless I put the complete path,e eg:
http://example.com/assets/images/photo.jpg
This isnt such a big deal changing the image paths, but it totally breaks Wayfinder.
Does anyone know a way to fix this?
My templates were missing:
<base href="[[++site_url]]" />

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.

MODx Relative Links Broken

I was having a lot of trouble getting relative links working in MODx. As soon as I made a container and put some pages one level above the root, nothing was linking correctly. Lots of missing images and broken links.
NOTE: This fix apparently will break all links linking to content identifiers.
I fixed it by putting the following at the top of the web template:
<base href="[[++site_url]]" />
You have to use that. If you hardcode it, it will screw up depending on which protocol you use, http or https.
StingyB's answer is correct. This tag should be in the head section of all MODX templates:
`<base href="[[++site_url]]" />`
Note that if you have multiple front-end contexts, the placeholder should be uncached:
`<base href="[[!++site_url]]" />`
Also, it must be a short tag. This will not work:
`<base href="[[!++site_url]]"></base>`
I'm not sure where comment about "breaking all links linking to content identifiers" is coming from. These tags are standard in all MODX installs.

Resources