How can i rewrite a image file name to query string via rewrite rules in Apache - apache

I am trying to create a php script that will check if a image is local and if not retrieve it from remote site. I want the user to call the image like this.
http://media.myimage.org/media/2AF97A30-42F7-4E08-8BF6-020316D6F161.jpg
To make it work i need to create a rule that will rewrite the url to the following request
http://media.myimage.org/media/image.php?id=2AF97A30-42F7-4E08-8BF6-020316D6F161
i tried multiple ways and tested them via https://htaccess.madewithlove.be/ but they all fail with
rule was not meet
i want to take my regexpr '/([{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?)(.jpg)/m'
and if it is meet then translate it to 'image.php?id=${1}' if not i want to redirect to a 404 page not found

With some more research I was able to solve my issue. Not sure its the best way but it works. If anyone has a recommendation to improve I am all ears.
RewriteEngine On
RewriteBase /
RewriteRule ^/?([{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?+).(jpg|png|gif)$ /madia/image.php?id=$1&format=$3 [L]
RewriteRule (.*) /images/404.php?image=$1 [L]
The above will accept any image request as long as the image name is a valid guid and the file has the extension of jpg or png or gif. If this Rewrite rule is hit we will exit, if not we will do the 404 rewrite.

Related

Rewriting directory into a query parameter in htaccess

I converted a website I'm building into a web view app in iOs.
I would like to track visitors that use the app instead of the website by adding a directory to my URL.
For instance, the "about" page URL would go from "https://example.com/about/" to "https://example.com/app-ios/about/"
My question is how to write an htaccess rule that tells my server to go to the path "/about/" and skip the "/app-ios/" directory?
Also, I'd like to add ?app=app-ios in my query parameters.
The most promising thing I found was this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/node/(.+)$ /search/node/?app=$1 [QSA,NC,L]
But I'd need to specify what comes before "foo".. In my case, "app-ios" is at the beginning of the request uri, always.
Plus I don't want a redirection. I just want my server to read /app-ios/something/other-thing/ as /something/other-thing/?app=ios.
First one question for tracking I would recommend to make this over a Query Parameter (as you wrote) like ?client=ios, but this might be an own opinion.
For rewriting the URL you could do the following:
to remove from the IOS from the URL (not tested):
RewriteEngine On
RewriteRule (.*)ios/(.*) $1/$2?client_id=ios&%{QUERY_STRING} [L]

Apache Mod_Rewrite Scenario

I was wondering how I would do a complex mod_rewrite. Below is basically how I want it done.
If the user goes to:
-http://files.stuff.example.txt.r.site.com/doc.txt
Then the server would rewrite the url to:
-http://r.site.com/index.php?type=txt&username=example&dir=files.stuff&file=doc.txt
Better picture:
-http://[dir3-dir2-dir1].[username].[type].r.site.com/[file]
Rewrites to:
-http://r.site.com/index.php?type=[type]&username=[username]&dir=[dir3.dir2.dir1]&file=[file]
I created a colour coded image to clearly show what I mean:
(can't embed images) look here:
http://i.stack.imgur.com/24H8j.png
The first subdomains are a directory structure (shown in red), so the amount of subdomains can change.
I hope someone can provide me with a solution. Either using mod_rewrite or maybe another method. Thanks.
Provided that you have configured your DNS so that requested URL hits server where your application is (maybe wildcard DNS on your domain: *.site.com -> 123.45.67.89, if supported by your DNS server/hosting), you can create more or less complicated rewrite rule. I'd do it this way:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).r.site.com$
RewriteCond $1 !^index.php
RewriteRule (.*) index.php?subdomain_part=%1&file_part=$1
So in index.php you get $_GET['subdomain_part'] and $_GET['file_part'], which you can parse further to extract parameters according to your convention.
Of course, you can write more complicated regex to get URL parts extracted by mod_rewrite (I'm not such an regex expert myself). However doing parsing in PHP would be much easier and you can do better error handling (e.g. if URL is not formed properly).

Need to create a RewriteRule in .htaccess for a Magento website

I am currently trying to put a RewriteRule into my .htaccess file for my Magento website that will allow me to write a category URL in the following way:
http://mydomain.com/dir/<direction>/order/<order>/<Magento category URL path>.html
What I am basically looking to do is use my robots.txt file to make some of the category URLs not appear (specifically when you apply a different sort order to the category).
So let's assume I have the following URL:
http://mydomain.com/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
I would like that to be rendered just as if it the URL was:
http://mydomain.com/footwear/mens-work-boots/motorcycle-boots.html?dir=asc&order=sales_index
The code I have put in my .htaccess file is as follows:
RewriteRule ^dir/(.*?)/order/(.*?)/(.*?)$ $3.html?dir=$1&order=$2
For some reason, when I have this in there, I get a 404 error. Can someone point me in the right direction to make this work for me?
I tried with this on my server
RewriteRule ^(.*)/dir/(.*?)/order/(.*?)/(.*?)$ $4.html?dir=$2&order=$3 [R,L]
and when i issue request
http://myserver/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
i get proper redirection to
http://yuave.dev:81/footwear/mens-work-boots/motorcycle-boots.html.html?dir=asc&order=sales_index
May be you are missing [L] flag on your request.

apache .htaccess - cut a string from url and redirect

For some reason google indexed several pages of my website as:
http://myapp.com/index.php/this-can-be-enything/1234
Now, I want to redirect with apache .htaccess those pages to correct urls:
http://myapp.com/this-can-be-enything/1234
I've googled and tried many options but with no success.
Any tip will be helpful.
I've added to my .htaccess file following lines:
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
I don't know if this is best solution but works ok for me.
Two Parts of problem
To make Google aware that indexed page is moved to some other destination you need to handle that # apache level and issue 301 ( moved permanently )
Handler to handle the cached requested URL to new URL using the #1 handler itself.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.