HTACCESS how to mod_rewrite the website Asset File paths? - apache

With Apache, how to make a URL (lets say an Image Path) to be actually coming from another location. For example, i have a html page with an URL like:
apple.html, with the original codes inside:
<img src="http://www.myoldsite.com/assets/image/apple.jpg" />
<img src="http://www.myoldsite.com/assets/image/orange.jpg" />
But actually i want this path to be re-written to:
<img src="http://www.mynewsite.com/image/apple.jpg" />
<img src="http://www.mynewsite.com/image/orange.jpg" />
So when i access the apple.html page, the image should be coming from new location, without needed to go and change the actual codes inside the files each.
But actually i'm totally weak in .htaccess rules. When i used like following, it is showing error:
RewriteEngine On
RewriteRule http://www.mynewsite.com/image/(.*) /assets/image/$1 [QSA, L]
I am having thousands of pages like this but I am totally stuck with .htaccess rules.
What am i missing please? Please help.

I assume that both domains point to the same apache instance?
You seem to have switched the arguments for the RewriteRule. The syntax is:
RewriteRule UrlsMatchingThis ChangeToThis [params]
So in your case I would do something like this:
RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1 [R,NC,L]
This matches all requests where the part just after the tld is /assets/image/(anything).
The flags I have used is
R redirect
NC Ignore case
L Last rewrite rule for this request
You may want to add a condition to the RewriteRule like this:
RewriteCond %{SERVER_NAME} ^(www\.)?myoldsite\.com$ [NC]
RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1 [R,NC,L]
This will only rewrite your requests if the domain is myoldsite.com, with or without www.
Se http://httpd.apache.org/docs/current/mod/mod_rewrite.html for documentation of the rewrite module.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(image/.+)$ /assets/$1 [NC,L]

Related

URL Rewriting Add a folder name to URLs

I have an apache2 server.
My directory structure is - (below is inside - /some-folder/abc)
- app
- otherfolder
- pqr.php
- xyz.php
- js
- css
.htaccess is placed inside /some-folder/abc
Context root for virtual host is set till - /some-folder/
Now, I want my users to enter this URL - http://someserver.com/abc/xyz or http://someserver.com/abc/pqr
I want to open the page at - http://someserver.com/abc/app/xyz.php or http://someserver.com/abc/app/pqr.php
How can I achieve this using URL Rewriting mod_rewrite
This is what I have so far, which is not working -
Options +FollowSymLinks -MultiViews
#Turn mod_rewrite on
RewriteEngine On
#RewriteBase /abc
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /app/$1\.php [L] # this doesn't work either
# RewriteRule ^(.*)$ app/$1.php [L] # this doesn't work either
# RewriteRule ^abc/(.*)$ abc/app/$1.php [L] # this doesn't work either
Thank you for your help.
If possible, I would also like to take all query params using forward slash
ex - http://someserver.com/abc/app/xyz.php/xId/123/uId/938/sdh/8374 instead of http://someserver.com/abc/app/xyz.php?xId=123?uId=938?sdh=8374
There is no pattern for query params, they can be anything for an page. Is this possible by a generic mod_rewrite, or do I have to rewrite urls for each page, and each time my developers add a new query param?
This rule should work for you from /abc/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/abc/app/$1.php -f
RewriteRule ^(?!internal)(.+?)/?$ app/$1.php [L,NC]
RewriteCond makes sure that we have a corresponding .php file in /abc/app/ sub-directory.
(?!internal) is negative lookahead assertion to skip internal from this rewrite.
Also it appears you're using a relative URL for css/js/images e.g. src="abc.png" and your current URL is: /abc/xyz then browser resolves this relative URL to http://example.com/abc/xyz/abc.png which obviously will cause a 404 since your static files are residing in /abc/app/ sub-directory.
You can add this just below <head> section of your page's HTML:
<base href="/abc/app/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also as a practice to use absolute links instead of relative ones change relative link XYZ, to this one XYZ

htaccess mod_rewrite URL within subfolder

My question seems to be unanswered on StackOverflow, so here goes:
I want to rewrite the following URL using an htaccess file which is in the root folder.
The URL to rewrite is this:
http://www.domain.com/subfolder/item/12345
to this:
http://www.domain.com/subfolder/item.php?id=12345
However nothing I seem to do works. I can successfully rewrite the item.php URL if it is in the root folder using this:
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L]
... but not if the item.php file is in a subfolder!
You can use following .htaccess in your subfolder:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subfolder
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L,QSA]
Your rewritten URL would become: http://domain.com/subfolder/item/123
Here is an example for sending this URL (The one entered in the browser address bar):
http://www.domain.com/subfolder/item/12345 (Can be any number) to this rewrited URL:
http://www.domain.com/subfolder/item.php?id=12345
RewriteEngine on
RewriteRule ^subfolder/item/([0-9]+)/?$ subfolder/item.php?id=$1 [L]
Those directives will work only if there is a subfolder directory and in that directory there is in fact a file item.php

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

.htaccess to show a directory index.html without a trailing slash

I've got a Jekyll generated site running on an Apache server and I'm having some trouble getting my .htaccess file set up correctly. Jekyll places index.html files into folders which represent each page so my URLs currently look like domain.com/foo/
I'd like to remove that trailing slash from the URL so that it exactly matches what I had set up previously (and also because I think it looks better).
Currently the section of my .htaccess file dealing with rewites looks like:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
Options -Indexes
DirectoryIndex index.xml index.html
I have tried following the advice here but that puts me into a redirect loop.
Can anybody help me out? In brief, what I want is for a domain.com/foo URL to show the index.html file form the /foo directory and for domain.com/foo/ and domain.com/foo/index.html to redirect to domain.com/foo.
You should be able to use this to turn off the addition of slashes.
DirectorySlash Off
Note that the trailing slash is added for a good reason. Having the trailing slash in the directory name will make relative URLs point at the same thing regardless of whether the URL ends with "foo/bar/index.html" or just "foo/bar/". Without the trailing slash, relative URLs would reference something up one level from what they normally point at. (eg: "baz.jpg" would give the user "/foo/baz.jpg" instead of "/foo/bar/baz.jpg", as the trailing "bar" will get removed if it isn't protected by a trailing slash.) So if you do this, you probably want to avoid relative URLs.
To then rewrite the directory name to return the index.html you could probably do something like this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]
This checks if REQUEST_URI/index.html exists, and if it does performs an internal redirect.

I Cannot get rewriterule to work

I'm using VertrigoServ 2.27 on my laptop (localhost:8080).
Rewrite module is enabled and i tested it with alice.html and bob.html example
(http://stackoverflow.com/questions/6944521/url-rewriting-on-localhost)
and it works with .htacces inside www-subfolder. And I also put rubbish text inside .htaccess and
I got error from Apcheserver so rewrite mod is running and I cab use rules.
here is the rule: (inside /www/folder1/.htaccess)
Options +FollowSymLinks
RewriteEngine On
RewriteRule /(.*) /index.php?disp=$1 [QSA,L]
So when I put this url into browser my index page loaded ok.
http://localhost:8080/folder1/index.php
And the problem is here: When I request login via index.page(login page) and send url to localhost
server by cliking send-button
the url changed localhost:8080/login, it should be localhost:8080/folder1/login
how I can keep subfolder name in url?
and I want convert urls like this: www.best-food-of-the-usa.com/index.php?operation=top&state=arizona&
city=mesa&limit=10
to like this: www.best-food-of-the-usa.com/arizona/mesa/top10.html
Any help is appreciated. Thanks
\Jose
RewriteRule is behaving as the docs say.
From RewriteRule Directive Apache Docs
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
So, when in Directory and htaccess context the prefix /www/folder1/ will be removed. Also remember when matching with RewriteRule in Directory and htaccess context, the pattern will never begin with /.
So, your RewriteRule Should be:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) /index.php?disp=$1 [QSA,L]