.htaccess - URL rewrite help - url

I have this code in my .htacess file :
RewriteEngine On
RewriteRule ^article/(\d.*) article.php?id=$1
When I open a link like this:
http://localhost/mysite/article/10
The page will open but no images, CSS, JS file loaded, I guess the problem is that pages are going to this URL article/10, so every URL in the article.php is changing to new URL even after redirect works.
How I can fix this problem?

This depends on where your resources are stored and how they are linked in your article, your article will need to point to something like http://localhost/mysite/image to reference an image without it being redirected by your script

Related

Removing filename from url with htaccess

I have a single page website
http://picniclounge.com/parallax-page/index.html
I would like it to read picniclounge.com/ and not with the folder and index.html at the end.
How can I do this using htaccess?
You can use this in your htaccess:
DirectoryIndex /parallax-page/index.html

Broken images when hosting laravel in subfolder

So I have hosted my laravel app in a subfolder lets now refer the root of my project as subfolder. I have a constant base file which has header and footer of the website, I used link of images as
<img src="/img/img1.png">
so it goes to domain.com/img/img1.png instead of domain.com/subfolder/img/img1.png
and if I do
<img src="img/img1.png">
it works fine for the first page but when I navigate to subfolder/user/1 my images brake again because now they are finding images on subfolder/user/1/img/img1.png. I remember once I ran into such situation and someone helped me to set the path of images by adding one line in the header of the website but now I am not able to find it.
Check your root Folder is correct
To avoid this problem, make use of asset() or url() function:
<img src="{{ url(img/img1.png) }}">
Edit: if you plan to attach "subfolder" in url path, then try to edit .htaccess file:
RewriteEngine on
RewriteRule ^/img/(.*)$ http://%{HTTP_HOST}/subfolder/img/$1 [L,R=301]
The code above searches the image /subfolder/img/img1.png when /img/img1.png is found.

How to rewrite Rails assets path?

I began moving all the assets in my site to S3 and ran into a problem with my asset path.
There's a WYSIWYG editor on my site that includes images by absolute path, so when you add an image, it doesn't use the rails image_tag helper but rather adds an image like this:
<img src="/system/images/image_1.jpg" />
The problem is that in production the URL /system/images/image_1.jpg leads to a non-existant file.
Naturally, two solutions are to 1) replace the URL dynamically (gsub) when it's called and 2) to loop through the database and replace the urls.
A better solution, however, would be to rewrite the /system/images/image_1.jpg url to point to S3. How do I do that?
Thanks!

Asp MVC 3: Dynamically resolve relative resources in views

I have a javascript application that runs in a view (index.cshtml).
Problem:
The problem is all relative paths are relative to the current url, which would be ok in a simple html webapp but not in asp mvc. The js-app shouldn't have to bother whether it's served in a normal html file or via a asp mvc page.
I.e. http://www.domain.com/<controller>/<action>/ contains a script test.js. This script loads an external xml file searching relative to it ie. "data/data.xml". The resulting url reads http://www.domain.com/<controller>/<action>/data/data.xml. This isn't found.
Question:
Is there a way to route static files (images,..., maybe even js files) to the content folder like "~/Content/controller/action/<pathToFile>/"?
Any help appreciated!
Lg
warappa
PS: I know about Url.Content() but that doesn't fit here.
The solution doesn't require mapping - just a simple html tag in the header:
<base href="#(Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) +
Url.Content("~/content/controller/action/"))" />
Lg
warappa
EDIT
Some browsers need an absolute url - sample updated.
In you can use absolute URL addresses to access you static resources:
$('img').attr('src', '/Content/Pictures/picture1.png');
or
<script src="/Scripts/script.js"></script>
This way you will allways get the same resources relative to the page base address, no matter if you load the script in a /{Controller}/{Action}/{View}, {Area}/{Controller}/{Action}/{View}, a custom route or even in a static script html page.
Or perhaps what you're looking for is the use of css files, since CSS's url('<path>') resolves the addresses relative to the CSS file's location. You would just need to import the one CSS file that had all the resource (image?) file paths. Then the scripts could reference the distinct class names, thus not being location aware at all. This is what libraries like jQuery UI do. But again this would require a fixed folder structure relative to the CSS document.

URL with trailing slash breaks layout - how can I fix this without using base tag?

I am developing a website, and I have set up the following htaccess rules
RewriteEngine On
#RewriteRule ^([0-9]+)?$ index.php?page=$1
#RewriteRule ^([0-9]+)/([0-9]+)?$ index.php?page=$1&post=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.php?pagename=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.php?pagename=$1&post=$2
This ensures that instead of http://www.mysite.com/index.php?page=2 showing the page
I get to use the friendlier method of http://www.mysite.com/about-us
* note I have not included a trailing slash.
In the page my css files are included as:
<link href="css/style.css" rel="stylesheet" type="text/css" />
and located at www.mysite.com/css/style.css
And this works well, however if I want to include a trailing slash (i.e. http://www.mysite.com/about-us/)
Then my css files do not load and I get an error where the Firefox source browser says:
The requested URL http://www.mysite.com/about-us/css/style.css was not found on this server.
This is because the page is determining about-us to be a directory instead of a page.
I am not keen to use the basehref tag like <base href="http://www.mysite.com/" />
Are there any other options?
Relative URLs are resolved from a base URL that is the URL of the document the relative URL is used in if not specified otherwise.
Now to fix this incorrect reference, you have two options:
change the base URL using the BASE element,
change the reference
by adjusting the relative URL path to the base URL path, or
by using just an absolute URL path, or
by using an absolute URL.
Since you don’t want to use the BASE element, you will probably need to adjust the URL you are using to reference the external resource.
The simplest would be to use the absolute URL path /css/style.css instead so that it is independent from the actual base URL path.

Resources