My htaccess Stopped Working - .htaccess

I'm using .htaccess in order to forward all image requests to a PHP script that will resize and crop the provided image before returning it to the request. Here's an example:
Let's say I want this image, the default size of which is 900x100:
http://www.mysite.com/images/header.jpg
Well, I want it sized and cropped to 500x95. So I instead make this request:
http://www.mysite.com/images/header_500x95.jpg
I'm using htaccess to manage this. Here's the code I'm using:
RewriteRule ^(.*)_(.*)x(.*).jpg image.php?u=$1&w=$2&h=$3 [PT,L]
You may be able to tell that I mean for htaccess to strip out the filename and dimensions and pass them as variables to the image.php file.
This was working up until today, when it suddenly crapped out. I'm not sure what went wrong. Any ideas?
Here is the full .htaccess file:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)_(.*)x(.*).jpg image.php?u=$1&w=$2&h=$3 [PT,L]

For the request URL
http://www.mysite.com/resizedimages/header_500x95.jpg
your rewrite rule
RewriteRule ^(.*)_(.*)x(.*).jpg image.php?u=$1&w=$2&h=$3 [PT,L]
would actually match resizedimages/header as $1.
Change it to
RewriteRule /?(.*)_(.*)x(.*).jpg$ image.php?u=$1&w=$2&h=$3 [PT,L]

Related

htaccess - the second url param is not visible

example.com/play/music to example.com/play.php?c=music
here is my code which works:
RewriteRule ^play\/music$ play.php?c=music [L]
now I have an extra url param (id as integer) added:
example.com/play/music/54
need to be - example.com/play.php?c=music&id=54
here is my beginning try:
RewriteRule ^play\/music/\d+/?$ play.php?c=music&id=$1 [L]
the page is there but problem is with id param - php doesn't see it
could someone help and explain ?
also, maybe is possible to join both rules (with and without id) in one ?
With your shown samples, please try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs and make sure your .php files(eg: play.php etc) are residing along with .htaccess rules file.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?c=$2&id=$3 [QSA,L]

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

modrewrite -f being ignored

Ive got a image cache system that im using to generate certain image sizes as require. Im using modrewrite to check for the file and call the image creation system if it doesnt exist. The problem im having is that it is calling the image creation system even if the image does exist!
The modrewrite is simple enough (sites in the images sub directory)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /images/index.php?id=$1 [L,QSA]
I even tried removing the -d incase the directory check was confusing it. Ive now got it so that it checks the file exists inside the index.php code - which reports that it does.
I use the url images/cache/140x20/Test.JPG in the html - the file exists, so the RewriteCond should not call the index.php code - but it does for some reason?
Im sure its something obvious but i just cant figure it out
the .htaccess is in the images directory. Using R=302 i get the correct path expected www.domain.com/images/index.php?id=cache/140x20/Test.JPG
That's weird. I was testing under a subdirectory and was getting some unexpected values for %{REQUEST_FILENAME}. But I was able to get the check to work correctly by reconstructing from the document root. Maybe my understanding of %{REQUEST_FILENAME} or the underlying mechanism apache uses to make URI-path to file-path is different than what I expected.
Try:
RewriteCond %{DOCUMENT_ROOT}${REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}${REQUEST_URI} !-d
# and maybe for good measure
RewriteCond %{REQUEST_URI} !index.php
EDIT:
If your extensions are in all caps but the image filename extensions are always all lowercase, you can use a RewriteMap and define a map using the tolower function:
(somewhere in your server or vhost config:
RewriteMap lowercase int:tolower
Then in your htaccess file (above your other rules), change the filenames to lowercase:
# first check to make sure request doesn't already have all lowercase image extension
RewriteCond %{REQUEST_URI} !\.(jpe?g|png|bmp|gif)$
RewriteRule ^(.*)\.(jpe?g|png|bmp|gif)$ /$1.${lowercase:$2} [NC,L]
You can also do something similar with the entire filename, or the entire path.

.htaccess 2 Dyanamic Rewrite URL

I have a dynamic url at
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
where the '|tag|' and '20' part change depending on the tag. This is Movable Type blogging platform which is written in perl. The '|tag|' part is a place holder for the tag. For example, if the tag was 'there' the url would be
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%there&limit=20
and not
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
I was wondering how to rewrite that in htaccess because everything I try doesn't work. I want it to be
http://www.technicae.net/tag/|tag|
instead of
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
can you please help me?
note
The URLS are not functional.
This is what you're searching for :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tag/([^/]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1 [L]
RewriteRule ^tag/([^/]+)/limit/([^/]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1&limit=$2 [L]
This will allow thos kind of url :
www.domain.com/tag/<myTag>
www.domain.com/tag/<myTag>/limit/<myLimit>
But not :
www.domain.com/limit/<myLimit>
Assuming you are using Apache, make sure you have mod_rewrite enabled.
The .htaccess you should use (this may need tweaked) is:
RewriteEngine On
RewriteRule ^tag/([^\.]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1&limit=20 [NC,L]

Kohana URLs including index on redirects and pagination

I am having an issue with the KO3 core inserting index.php into my URL's when I use redirect
Request::instance()->redirect('something'); or $paginationStuffHere->render().
The result of either of these is http://www.something.com/index.php/something
This is not an issue when I use full URL's for the redirects instead of relatives such as Request::instance()->redirect('http://www.something.com/something'); but there is not really a way to do this with the pagination functions... that I have found, so I really need to find where it is adding this index.php
This does not occur when I use View::factory('something/something')->render(); which is the only thing I have been able to find people having similar problems with
The base URL is set to '/' in bootstrap.php
My .htaccess looks like this
RewriteEngine On
RewriteBase /
RewriteRule ^(application|modules|system) -[F,L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule .* index.php/$0 [PT,L]
Thanks in advance for any suggestions.
Find Kohana::init() in your bootstrap.php and set index_file to FALSE in that array

Resources