This is a simple htaccess question for experts but I have been trying to get this sorted for a while. This was something a developer did before my time with this code. He truncated the image file extension from the requests. As an example,
/images/btn/Find a bear should get the URL changed internally to /images/btn/Find a bear.gif
All the images in the folder are .gif extensions.
I tried this URL rewrite at the root folder but it did not help.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^images/(.*)$ images/$1.gif
I know that RewriteRule is enabled on the server, etc. Please help.
You’re probably having an infinite loop. Try this:
RewriteCond $1 !.*\.gif$
RewriteRule ^images/(.*)$ images/$1.gif
Related
I've installed WAMP, made an alias, and put into that folder a ".htaccess" file. My goal is to have a URL such as "foo.com/blog/bar-baz" internally call "display.php?name=bar-baz". Among various things I tried the following:
RewriteRule ^blog/(.+)$ display.php?$1
However, this gives a "The requested URL /Users/.../public/display.php was not found on this server". Playing around I was able to get this to work:
RewriteRule ^blog.php$ /personal/display.php
Advice greatly appreciated.
You can try this rule from root .htaccess:
RewriteEngine On
RewriteRule ^blog/(.+)$ /personal/display.php?name=$1 [L,QSA,NC]
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
I have created several php files that dynamically build images for user rankings. I included an htaccess file in the image directory to translate the php files into png files so that the phpBB board would allow them to be used as ranks. Up until yesterday everything was working fine. I was using the following code in the htaccess file:
RewriteEngine On
RewriteRule ^rodeojones.png$ rodeojones.php
RewriteRule ^marine.png$ marine.php
RewriteRule ^treble.png$ treble.php
RewriteRule ^major.png$ major.php
RewriteRule ^hyghway.png$ hyghway.php
RewriteRule ^zypher.png$ zypher.php
etc... Now, all of the sudden, it isn't working. I am guessing this isn't the most efficient code and therefore it is no longer working for that reason, but I am not sure. This was my first adventure in php so I am lost as to a solution. Any help would be greatly appreciated. Thank you :)
Try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^(script)/([^.]*)\.php$ $1/image.png [L]
Hopefully will help you, though needs to find more specific to your scenario !
This question seems simple to me though I can't seem to get it to work.
I'm trying to do a simple rewrite for all requests except defined image types.
Three very important clarifications:
This is being done with the .htaccess file in the image directory that accessed from a browser is located at example.com/images/ so the .htaccess file is located at public_html/images/.htaccess locally.
This solution needs to play nicely with the awesome help I received from other people at this question: Apache Rewrite: image directory based on HTTP host
I'm using shared hosting, not VPS or dedicated so my access is limited; I know this isn't going to be complicated anyway.
I've tried the following (along with modifications of it) without success:
public_html/images/.htaccess
RewriteEngine on
RewriteRule !\.(gif|jpg|png)$ index.php?q=$1 [L]
I think I'm probably just missing something simple here.
I presume you are trying redirect everything to index.php when the request to images folder is not a request to an image file.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$ [NC]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question:
I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically.
My directory structure looks like this:
projects
project1
.remoteindex
.info
project2
.remoteindex
.info
index.php
.htaccess
So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.
This is what I tried:
.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever.
I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.
Edit:
See my answer below for the used .htaccess.
The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?
Thanks for all the help so far! You guys are just awesome!
I figured out that a symbiosis of both of your solutions works well for me:
RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]
Of course only without [R], this was my fault. (See my question edit for another question please).
You need to add a RewriteBase /projects to the htaccess.
That way the redirect will work properly.
Edit:
RewriteEngine on
RewriteRule ^index.php - [L]
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
Following what you ask, this is important not to do a redirect, but let the rewriterule modify internally the URL i.e. the RewriteRule should not end with [R,L] but rather [L] and maybe the "query string append" directive to keep what's after the ? so this should probably be [QSA,L].
Now here's how I'd do to avoid rewriting static files: if it's not a file then (and only then) test it:
# if it's not a file...
RewriteCond %{SCRIPT_FILENAME} !-f
# ... and it's a dir
RewriteCond %{SCRIPT_FILENAME} -d
# ... then rewrite it internally and stop further processing:
RewriteRule projects/([^/]+)(/?)$ index.php?dir=$1 [QSA,L]
And now two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)