handle image URL (.htaccess) by passed variable - apache

I would like to handle my image URLs in two different ways.
First, when the browser calls for:
http://example.com/image.jpg (need to be shown)
Second, when the browser calls for:
http://example.org/image.jpg?actionId=123 (need to be URL rewrote to index.php?actionId=123)
The point is, when the URL has it's specified variable, then we need to redirected to the index.php with that variable and it's value.
I'm using .htaccess with mod_rewrite, which is the following now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
</IfModule>
So it's checks for existing file or directory, any other way, it's passed to index.php in 'q' param.
Thank you in advance!

For the first example you don't need to do anything, the image will be shown if it exists. Therefore you only need to consider the case with the query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} actionId=([^&]+)
RewriteRule \.jpg$ index.php?actionId=%1 [L]
If there are other parameters that you also need to pass to to index.php then just doing the rewrite should be enough as the query string is sent by default. To be sure use the QSA flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} actionId=([^&]+)
RewriteRule \.jpg$ index.php [QSA,L]

Related

How to capture the requested url and pass its substring to some other link in .htaccess

I am making some changes in .htacess file and i want to capture the requested url, create substring of it and pass it to third part url.
For example
I am working on ubuntu, so currently my .htaccess file is in folder test. So when i access localhost/test/http://www.facebook.com , it takes me to service.prerender.io/test/http://www.facebook.com but i want to eliminate the 'test' and forwrd the request like service.prerender.io/http://www.facebook.com
Here is the code of my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^(.*)$ http://service.prerender.io%{REQUEST_URI} [L,QSA]
</IfModule>
You can use the RewriteCond %{REQUEST_URI} to set capture groups and instead of rewriting to http://service.prerender.io%{REQUEST_URI} you can rewrite to something like this http://service.prerender.io/%1.
Here is an example (untested):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteCond %{REQUEST_URI} ^/?test/([.]+) [NC]
RewriteRule ^(.*)$ http://service.prerender.io/%1? [L,QSA]
</IfModule>
What you are trying to captures is not directly the requested uri but part of it. You can get it from rewrite rule
Try following
RewriteRule ^test/(.*)$ http://service.prerender.io/$1? [L,QSA]
If you're accessing a page something like,
https://www.example.com/thisdir/thisfile.php
Then you will get following as request uri and script uri
REQUEST_URI: /thisdir/thisfile.php
SCRIPT_URI: https://www.example.com/thisdir/thisfile.php
And https://github.com/prerender/prerender-apache is more information about htaccess and prerender

.htaccess get lost query string

I have limited .htaccess knowledge and i am requiring some help. I need to do some redirects to enable pretty urls.
In local all works fine but it is not working in another develpment server. apparently the query string get drop when redirect.
i need to redirect this http://mysite.local/info/permalink/1
to this one http://mysite.local/info?proxy=true&id=1
my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#remove /index.php from home url and redirect to root.
#http://mysite.local/index.php -> http://mysite.local/
RewriteCond %{THE_REQUEST} ^.*\/index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L,QSA]
#pretty url without index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [PT,QSA,L]
#rewrite to handle some permalink saved on my db.
#http://mysite.local/info/permalink/1
#http://mysite.local/info/proxy=true&id=1
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$3 [L]
</IfModule>
the redirect is working but the query string is not present. When I run var_dump($_GET) on info module i am getting an empty array array(0) {}
i have try it to solve changing
RewriteRule .* index.php [PT,QSA,L]
to RewriteRule ^(.*)$ /%{QUERY_STRING} [L]
and
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [L]
to
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [QSA,NE,L]
Server API is CGI/FastCGI
What should I change to ensure that my rewrite works as intended and $_GET variables still are accessible?
thx in advance
I have no idea how you've managed to get this to work with a regex pattern like: ^([^/]+)/info if the URL you are going to is /info/permalink/1.
The ^([^/]+)/info pattern means there's stuff before the /info part of the URI, which there isn't. Additionally, in an htaccess file, the URI's have the leading forward slash stripped off. So you probably want something like this:
RewriteRule ^info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$2 [L]

Url renaming using .htaccess file

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!
I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteRule ^fileThathasalongname.html file.html

.htaccess question - URL-rewriting

I have an issue with URL-rewriting in .htaccess. Here is .htaccess file:
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results$ forums/index.php?view=view-all-results [R=302]
RewriteRule ^view-all-results/$ forums/index.php?view=view-all-results [R=302]
I need to rewrite url like "/community/view-all-results?u=2" to "community/forums/index.php?view=view-all-results&u=2".
But according to the above rule I'll get "community/forums/index.php?view=view-all-results".
I tried to change RewriteRule to
RewriteRule ^view-all-results?(.*)$ forums/index.php?view=view-all-results&$1 [R=302]
But it doesn't work properly. It still rewrites URL to "community/forums/index.php?view=view-all-results".
When I changed rule(put + instead of *):
RewriteRule ^view-all-results?(.+)$ forums/index.php?view=view-all-results&$1 [R=302]
I've got URL like "community/forums/index.php?view=view-all-results&s". So I don't understand this behavior.((
I will be very appreciated for any suggestions.
The magic flag is in the docs: [QSA], which will add the original querystring to your url.
Normal matching is only done against the path, not agains the querysting, which you would find in the magic variable %{QUERY_STRING}). Matching this variable can be done in a RewriteCond condition. You could also append this variable to the resulting url, but QSA is infinitely more userfriendely here.
Give this a try...
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results/?$ forums/index.php?view=view-all-results [QSA]
Basically the first half of a RewriteRule doesn't match against the QUERY_STRING, so you second to example will never match against it. The main thing your first code was missing was the QSA flag, which tells it to pass the QUERY_STRING it receives along with the newly created QUERY_STRING. I also removed the R=302, as I assume you don't want the URL to change.
Edit: Oh, I also combined the rules by making the trailing slash optional.

How would I write a RewriteRule to make an exception for one file

I currently have this in my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.html$ index.php?pagename=$1&%{QUERY_STRING}
The the html file name is rewritten to the pagename query string.
However I'm trying to allow access to one particular static html file, so somehow I need to overwrite the rule or make an exception.
Thanks for you help.
No point appending QUERY_STRING yourself; you'll leave a stray & if there isn't any, and mod_rewrite already has a tool to do it better:
RewriteRule ^(.*)\.html$ index.php?pagename=$1 [QSA]
You can control a RewriteRule with a RewriteCond that precedes it. For example:
RewriteCond %{REQUEST_URI} !^/staticpage.html$
RewriteRule ^(.*)\.html$ index.php?pagename=$1 [QSA]
Another useful pattern is RewriteCond %{REQUEST_FILENAME} !-f which will bypass the following RewriteRule any time that the URL would have matched an existing regular file in the docroot.