.htaccess rewrite url - show html file as jpg - html

I have image.html and I want It to be shown in browser as image.jpg, because there are some operations when showing picture, that can't be done before or after. Is it even possible with .htaccess and url rewriting?
I tried
RewriteEngine On
RewriteBase /
RewriteRule ^image\.html$ image.jpg
but without any luck.
Thanks for all answers

First of all, you'll need let's say a PHP script. Call it image.php. After that, use a GET parameter to handle the image name. Let's call it name. To call a picture you need image.php?name=allo.
Now. In the PHP script, you need to specify in the header it's an image, JPEG. Required. After that, just "print" the picture within the page.
All you need after is Rewrite rule.
RewriteEngine On
RewriteBase /
RewriteRule ^image\.php?name=(.*)$ /images_jpeg/$1
The header is really important. This is why it doesn't work with an HTML file.

Related

Make url so that I can also accept extra parameter in .htaccess

I have goggling all around but not able to find the right solution.
I have a simple HTML site that contains only HTML files(.html extension) like pricing.html,contact-us.html etc.
I want to make url so that it accept integer as a parameter like this
Currently: http://www.amznhosting.com/pricing.html
I want to make url so that I can also accept extra parameter like this:
http://www.amznhosting.com/pricing.html/2
This is what I did so far in .htaccess
RewriteRule ^(.*)$ pricing.html?params=$1 [L,QSA]
Its working but css,js and images are not getting load.
Anyone having any idea?
You can use the following rule :
RewriteEngine on
RewriteRule ^(.+)\.html/(.+)$ /$1.html?params=$2 [NC,L]
This will rewrite /file.html/path to /file.html?params=path .

htaccess- only one page redirects from html to php

This is normal question, but i am very weak regarding .htaccess
I have one website in html.All Pages are in .html. Now only enquiry page is enquiry.php
So I want enquiry page from enquiry.php to enquiry.html in URL.
It should not affect other html files
Pls help
Provided you've already turned "RewriteEngine On" your rule might looks something like this:
RewriteRule ^enquiry.html$ /enquiry.php [QSA,L]
However depending upon your web host, that may not work out of the gate. For example, you might need to add a slash before the .html so it would read:
RewriteRule ^enquiry\.html$ /enquiry.php [QSA,L]
I've also seen where you may need to add "Options FollowSymLinks" to the top of the .htaccess file.

How to link to pages without the .html extension?

I would like to link to pages inside my website, e.g:
Not: mywebsite.com/about.html But: mywebsite.com/about/
I've seen various websites doing this but it looks like they also react differently to things:
Apple.com:
apple.com/iphone/ works, apple.com/iphone/index.html works, apple.com/iphone redirects.
Opera.com:
opera.com/mobile/ redirects, opera.com/mobile works, opera.com/mobile.html does not work.
Mozilla.com:
mozilla.org/en-US/ works, mozilla.org/en-US redirects, mozilla.org/en-US/index.html does not work.
Which leads to another question: Are there different methods for this?
Edit:
It seems that Apple uses a folder for every page, e.g. a folder called 'iphone' with an index.html file inside it?
But Opera and Mozilla use something in the .htaccess file?
Removing Extensions
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper
They are using .htaccess and URL rewriting. This is part of server configuration. You can not do it with html only.
This page explains basics of URL rewriting.
You folder then has to contain a file: index.*.
Like: /iphone/index.html, which can be /iphone/ as well
Or work with .htaccess
In the .htaccess file in your sites root folder just add the following line:
# ---- Render pages without urls
Options +MultiViews
The most upvoted answer doesn't check whether the URL points to a directory, so you're going to get some mysterious 'not found' errors when it tries to append '.html' to a directory path. Easily fixed:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
The first condition will only pass if the path does not point to a valid directory. The second will only pass if the path points to a valid file when the .html extension is added. If both conditions pass, the rewrite rule simply adds ‘.html’ to the filename.
Notice that we can just match the entire path with .*. You can reject paths that contain a period character if you wish, but it's not really necessary since you've already checked that {REQUEST_FILENAME}.html is a valid file. In any case, it is unnecessary to escape a period character when it's inside a character class. I know you see this [^\.] everywhere, but the slash is redundant. [^.] is how to write it and look like a regex pro. 😎
This kind of redirect will be invisible to the user because, by default, mod_rewrite does the substitution internally, without informing the browser. If you wanted to do a permanent redirect, you would add the [R=301] flag at the end.
Alternatively, as Genus Amar said, you can just enable the Multiviews option on a per-directory basis by adding this Options Directive to the .htaccess file:
Options +MultiViews
It's worth adding that this will only work if the server administrator has enabled MultiViews with the AllowOverride Directive, and it won't allow you to perform additional redirects.
Neither of these solutions (on their own) will remove the .html if it’s part of the requested URL. If you want to do that as well, see my answer to this question.
Make your href attribute equal to the page you want to link or .. If you need to
move up a directory.
Ex: href="contact.html"
Ex: href="../links/contact.html"

Can we block opening CSS, JS files?

It is very common we link CSS and JS files in out HTML, PHP pages. Can we block opening of the CSS and JS files directly from a browser. Since the source code can be viewed by anyone, he/she can open those files by understanding the path. How can we achieve blocking these files?
You need to setup your web server to server the static content (js, img, css) only if refered by your host (looking at the http headers), but it won't totally prevent user from doing it. as for the php users won't see it, it runs on the server, and will output most of the times inert html.
A basic block would force the browser to send a valid Referer header when accessing the files. This can be done with some simple .htaccess:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !your\.domain\.here [NC]
RewriteCond %{REQUEST_FILENAME} \.(css|js) [NC]
RewriteRule .* - [L,F]
However this is not a great idea because it's easy enough to fake the referrer, or add a link to the page so that the browser naturally sends it. Also, some browsers just don't show the referrer header.
The browser has to be able to access those files in order to properly display the page. You can obfuscate the JS, either through something basic like minification, or something more complex like How can I obfuscate (protect) JavaScript?
With CSS, you can try something similar.
Look into the following resource, they might help.
http://www.iwebtool.com/html_encrypter
http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php
http://refresh-sf.com/yui/
http://www.n1studios.net/tutorials/php/css-file-protection.html
all of them are somehow way around, and there is always a way to read them. you can only make them hard to read

Absolute Path for Deployment in a Non-Root Location

I typically refer to any assets on my site using absolute path so that I don't have to worry about the location of the assets relative to current file.
<!-- Using a absolute path. -->
<img src="/images/flag.png" />
<!-- Using a relative path. -->
<img src="../../../images/flag.png" />
However, this time I need to host the site at a non-root location e.g. http://my-server.com/holiday/.
How would I go about this? I am looking for a solution that doesn't require me to change the path in my files. How do I configure the server (Apache) to treat http://my-server.com/holiday/ as a "root"?
Clarification:
I still need http://my-server.com/ to behave "normally". That is, it should still point to http://my-server.com/index.html i.e. doesn't get redirected to http://my-server.com/holiday/.
If you’re using Apache, there one rather simple thing you could do by just using an SSI variable in your paths.
Do a global replace of all src="/ to something like
src="<!--#echo var="prefix" -->/
and then in your htaccess for the specific folder define the prefix variable as /holiday
For sites that don’t have the variable or SSI, it’ll just show up as a comment or you can define it as an empty string.
Of course this means you’ll have to turn on SSI in Apache.
try to add this to your .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?DOMAINNAME.com$
RewriteCond %{REQUEST_URI} !^/holiday/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /holiday/$1
RewriteCond %{HTTP_HOST} ^(www.)?DOMAINNAME.com$
RewriteRule ^(/)?$ holiday/index.php [L]
or take a look here - Changing the root folder via .htaccess
I don't think this is gonna happen for you man, sorry.
You have absolute paths in your documents, and want apache to prepend /holiday to them all without effecting the actual docroot? You can have one or the other, but not both.
You need to either do a mass edit and prepend the directory yourself, or move your images/css/etc into the actual root directory.
mod_rewrite is powerful, but can't really determine intent and parse the same url two different ways depending on what the user wants.
Edit:
I am wrong, but I don't have your answer. You may be able to use IS_SUBREQ in mod_rewrite to only apply the re-write conditions for sub-requests from your /holiday/index.php
Easy...
# Assuming mod_rewrite is an option...
<IfModule mod_rewrite.c>
# Turn it on!
RewriteEngine on
# If path is /images/flag.png, connect to /holiday/images/flag.png
RewriteBase /holiday/
</IfModule>
Assuming I'm understanding what you mean, this should do you just fine. Point of order, this .htaccess should be in /holiday/
I do this locally on MAMP for testing a website that's base is in http://localhost:8888/SocialNetwork/ ... If I didn't have that, my absolute paths of, say, /profile would go to http://localhost:8888/profile/ instead of http://localhost:8888/SocialNetwork/profile
what about using html's BASE element? http://www.w3.org/TR/html4/struct/links.html#h-12.4
although i´m not sure how you could have it in a single html file and inherit it to the rest of your htmls, so your source remains intact.
if your site is html-only maybe with frames, otherwise you could use some sort of server-side include depending on what youre using (asp, jsp, whatever). Check out this link for more information http://www.boutell.com/newfaq/creating/include.html
I hate to say this but none of the above provides the solution. The answers by #Zoltan and #stslavik were close but, unfortunately, didn't work when deployed; I wished one of them worked, though.
As a result, I resorted to using a combination of named constants and include files in PHP. More details: File Structure for a PHP Project. Note that it doesn't have to be PHP; you can use other languages that provide similar features. +1 for #margusholland whose answer led me to experiment with this solution.
EDIT:
Of course it would work in case you do with php. Adding:
<? define(_BASE_, substr($_SERVER['SERVER_NAME'] . "/" . $_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/" )) ); ?>
<html>
<head>
<script src="<?=_BASE_?>/path_relative_to_project_root"></script>
</head>
<body>
<img src="<?=_BASE_?>/path_relative_to_project_root">
</body>
</html>
If you have a list of filename extensions that should be redirected, you might want to use the rewrite conditions using pattern matching againt the extensions.
Another solutions is to simply create an /images directory under root and let images be downloaded from there. Or have there links to images in your path.