I have a script called image.php that does some image manipulation like resizing and optimization. I already wrote .htaccess to omit the .php at the end and now I have something like this:
http://www.example.com/images/imageFolderPath/myImage.jpg?width=100&keep_aspect=1&quality=80
where the parameters were supposed to be passed to images.php and be used to manipulate imageFolderPath/myImage.jpg.
My problem is I don't know how to write .htaccess that would read the imageFolderPath/myImage.jpg as a parameter and also pass along other parameters if exist.
I got the whole idea from AirBnB website, how they manipulate their images like this
https://a1.muscache.com/ic/discover/94?interpolation=lanczos-none&output-format=jpg&output-quality=70&v=33b4f2&downsize=326px:326px
This is my .htaccess so far:
#RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1
Insert this rule just below RewriteEngine On line:
RewriteRule ^images/(.+)\.(jpe?g|png|gif)$ images.php?image_path=$1.$2 [L,QSA,NC,R=302]
You can analyse the request URI in your php using:
$_SERVER ['REQUEST_URI'];
It will contain information about the path requested by the user, it should include also the filename.
Related
I have a URL https://testing.in/show_tmp?tmp=abcval. I want a clean URL like https://testing.in/abcval without breaking the functionality of the site. My website is built in Core PHP. I know we can achieve this using the htacess file but i don't know how to do it please help me out.
htaccess file code
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA]
We add a rewrite condition as to current request URI should not contain show_tmp to avoid too may redirects issue. Later, we just capture the request URI and redirect to show_tmp
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA,P]
Demo: https://htaccess.madewithlove.be?share=0ddbbf44-57e1-5c9b-9e83-500961e8050d
After assisting with Anydesk access, your final .htaccess should look like below:
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f #add this for any kind of file extension. If you want to display .html, then you will have to add .html also
RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}/show_tmp?tmp=$1 [NC,L,QSA,P]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
I know with .htaccess, you can request a file without the extension, like (page.php) can be requested as (page).
The following is the code in my .htaccess file to enable this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This code is only able to work when just (page) is requested. How do I make it so that by request, (page.php) is forcibly rewritten to just (page)? Is there anyway of doing this?
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\s|\?) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)/?$ /$1.php [L,QSA]
Note: i wrote this code to make it work with /some_page.php but also with /folder/subfolder/.../some_page.php
I have a htaccess file that I am try to make a rewrite but it causes me multiple problems. First here is my htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2&%{QUERY_STRING}
RewriteRule ^login$ login.php?category=$1&%{QUERY_STRING} [L]
The first problem is that when I make a request like "mysite/foo/bar/", it does rewrite the query string in category and itemId but it also changes the base directory for my css files and Js files to /foo/bar/design/css/style.css
The second problem is that I have a few path like "login/","register/","users/", and when I try to load the corresponding file, it says:
The requested URL /login/ was not found on this server.
Thanks for the help!
First change your .htaccess like this:
RewriteEngine On
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2 [L,QSA]
RewriteRule ^login/?$ login.php?category=$1&%{QUERY_STRING} [L,QSA]
i have a code in my .htaccess file.. It redirect every .php to non php. I want it to only direct one php file and dont redirect the rest.. forexample i want abc.php to be abc but bcd.php stays as bcd.php.. How can i modify this script to get this result? thanks.
RewriteEngine on
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect .php to non-php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.php$ $1 [R=301,L]
This code should work for you:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(abc)\.php[?\s] [NC]
RewriteRule ^ %1 [R=301,L]
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Important to use %{THE_REQUEST} here which represents original HTTP request as received by Apache to avoid looping. %{THE_REQUEST} doesn't get rewritten with various rewrite rules as opposed to the case with URI pattern used for RewriteRule.
Put the rule with exception and L flag before the general RewriteRule:
RewriteEngine on
#redirect abc.php to abc
RewriteRule ^abc\.php$ abc [R=301,L]
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ! abc$
RewriteRule ^(.*)$ $1.php [L]
I hope you can help. I'm trying to write a .htaccess file to do the following.
redirect to www. address
remove .php from URL
If the file doesn't exist then use filechecker.php?page=filename
#1 I can do with
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#2 I can do with
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]
#3 I thought
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([^/]*)$ filechecker.php?page=$1 [QSA,L]
would work, but for some reason it is ignoring the fact that the page does actually exist.
Your solution for 2 will loop, but simple enough to fix, stick something along the following lines in your file for part 2 and 3:
#If the Browser request contains a .php, instruct the browser to remove it.
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule ^/?(.*)\.php$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
#If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !^\.php$ [NC]
RewriteRule ([^/]+)$ - [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]
#See if the file exists with a .php extention, if it does internally rewrite
RewriteCond %{ENV:testScript} -s [OR]
RewriteCond %{ENV:testScript} -f
RewriteRule .* %{ENV:testURI} [L]
#Else if a ENV:testDile is set, then pass the name to the php script
RewriteCond %{ENV:testFile} !^$
RewriteRule .* /filechecker.php?page=%{ENV:testFile} [L]
FYI: The Apache mod_rewrite documentation is worth a read, if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.