.htaccess and images redirect from another server - .htaccess

Wonder, I'm bulling a system that I will be able to upload to another hosting with domain just 1 file like: index.php and this file will calling to some other file in another server and print the html he got.
so right now, there is a option to do, when I print:
<img border="0" src="images/logo.jpg" alt="" />
something in the .htaccess or something will now that this image is on:
http://www.masterdomain.com/images/template_1/
?
I tried:
RewriteRule ^images/(.*)$ http://www.masterdomain.com/images/template_1/$1
but it's not working very well,
I need also css so I believe it's the same command.
Thank you all!

Try
RewriteRule ^images/(.*)$ http://www.masterdomain.com/images/template_1/$1 [NC,P]
The P flag will proxy your request to the masterdomain server

Related

Rewrite rules for localhost AND live environment

I want to add rewriterules that works in local environment (localhost) and on my liveserver.
Why?
I don't want to change rules when I test my project locally and upload it to the liveserver.
Adding Rules
Here an example
(ANY-URL)index.php?page=somepage
change to
(ANY-URL)/somepage
So I used a rewritemod generator and pasted this into it:
index.php?page=somepage
The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)
RewriteRule ^([^/]*)$ /?page=$1 [L]
When I try to get to (http:)
//localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.
Testing
Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works.
Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php
Possible solutions?
Maybe it has something todo with that "RewriteBase"?
Do i have to set a basepath?
My .htacces is located here:
//localhost/myproject/development/.htaccess
Later I also want to change paths that look like this:
(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool
Also here a I'm looking for a solution that works on both environments.
Since the htaccess is located inside a sub-directory, use RewriteBase:
RewriteEngine On
RewriteBase /myproject/development/
Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.
<base href="/myproject/development/" />
Now, the rewrites in htaccess would be:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

CodeIgniter Path Issue

I am use Codeigniter to creat my site. it is a huge site with a lot of contents.
i use the default welcome controller for all pages.
the path is like this now
http://mydomain.com/index.php/welcome
and my folder structure is like this
/root
/codeigniter Application folder
/controller
/view
/static/
/images/
/css/
/pdf/
.htaccess
because i am doing the content first, so all the images are set to absolute patch (http://mydomain.com/static/images/foldername/pc.jpg)
now when i use the rewrite to remove "inde.php" and "welcome"
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
RewriteRule ^(.*)$ /index.php/welcome/$1 [L]
all css and image file are not accessible anymore. I have nearly a thousand pages with images. I don't have the time to change the page one by one. the deadline is coming, please help.
thanks
Have you tried adding the css-folder to your excluded rewriteconditions?
RewriteCond $1 !^(index\.php|images|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
Does not seem to exclude the folder css from rewriting (see images is excluded).
Try something like
RewriteCond $1 !^(index\.php|images|css|robots\.txt|system|pdf|sitemap\.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*)\.pdf|phpadmin)
Good luck
I think the problem lies with your .htaccess file. We have a similar setup, and that url living outside of the application directory is available just as you have it with a direct url relative to its path. Check out the wiki at http://codeigniter.com/wiki/mod_rewrite for the correct way to set up your .htaccess file.
Edit to clarify: The url http://mydomain.com/static/images/foldername/pc.jpg should work. something about your .htaccess file is wrong.
RewriteCond $1 !^(static|index.php|images|robots.txt|system|pdf|sitemap.xml|profile.htm|^([A-z,0-9,_,-]+).asp|(.*).pdf|phpadmin)

How can I add ?1 to all .js and .css files on the server using .htaccess?

Our web host recently updated their media accelerator to start caching .js and .css files. In doing so, they broke some of our client's tools on their site because a .js file is not being loaded correctly. They told me that if I can get it to load with ?1 at the end of the URL, it should start loading correctly. However, I have no idea how to do this in the .htaccess. Can anyone help me with this?
This isn't a job for .htaccess.
You need to modify the references to the URLs to add ?1.
You can use .htaccess to cause the URLs to redirect to ?1 versions, but that won't do any good—the browser isn't requesting the URLs in the first place, because they're too cached.
You can put this code in your .htaccess to append ?1 to css and js files like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule \.(css|js)$ %{REQUEST_URI}?1 [R=301,L,NC]
As #SLaks stated, .Htaccess won't work.
Even if you did add ?1 on with htaccess, your browser would then cache that too.
If the JavaScript/Css file is cached there should be no problem with it as the browser would just load the cached files.
One way to get round it would be to do something like:
<script type="text/javascript" src="file.js?"<php echo date("ymdhis");?> />
without the need for .Htaccess

.htaccess to redirect "/whatever" links to a subdirectory

I coded a website assuming the test server would be something like (http://test.domain.com), however, it ended up being at http://www.domain.com/test. I don't have access to the hosting panel to create a subdomain, whatsoever.
The .htaccess file has to reside in /test/ and all links such as href="/css/style.css" should load from "/test/css/style.css.
Is this possible?
if you do something like this..
RewriteRule ^css/style.css$ /test/css/style.css [L]
as you can see, you can only redirect from that directory (where the .htaccess is at) and under, not above
so, i don't think it'll work, because you need access to / and not just /test/

Resources