Apache mod_rewrite help? - apache

I want to rewrite requests like:
mydomain.com/this/is/a/test
to:
mydomain.com/url.php?i=this,is,a,test
How can I write a rule that can handle this?

Create a .htaccess file with the below contents and place it in your root web directory.
RewriteEngine On
Options +Followsymlinks
RewriteRule (.*) url.php?i=$1
That would create a query string of this/is/a/test instead of your requested comma-separated value; that's nothing a quick string-replace function (like str_replace() in PHP) couldn't fix.

Related

.htaccess rewrite rule ignoring string, multiple GET variables in url

I am trying to make .htaccess rewrite rule to map 4 different get variables and exclude one string. String is unchangeable ie. always will remain same.
Current url is:
/car.php?make=bmw&model=z4&year=2006&color=black_metallic
It should be like this:
car/bmw-z4-2006-black_metallic-for-sale
This is I've done so far
RewriteRule ^car/^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4
Now I need to ignore string -for-sale at the end of the pretty url.
Any help greatly appreciated!
Try this
RewriteRule ^car/([^/-]+)-([^/-]+)-([^/-]+)-([^/-]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4 [QSA,NC,L]
Your delimiter is hyphen not forward slash hence your RewriteRule should also handle that accordingly:
Options -MultiViews
RewriteEngine On
RewriteRule ^car/^([^-]+)-([^-]+)-([^-]+)-([^-]+) car.php?make=$1&model=$2&year=$3&color=$4 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Using Apache Rewrite Rule to pass information from URL to Filename

I've been banging my head against this one and have unfortunately failed. I'm hoping somebody can help me.
I'd like to pass information from a URL to a filename. Basically I'd like to do the following:
There is only one real file on the server:
http://www.domain.com/download/file.zip
If a user enters the following URL into his browser:
http://www.domain.com/download/file_xyx.zip I'd like apache internally serving file.zip but the user downloading the file as file_xyz.zip
xyz is intended to be a variable (of any length) so the rewrite rule should simply accept anything it's place and always internally link to the same file.
I'm thinking this can be achieved with a rewrite rule, am I wrong?
Yes, you just need this rule:
RewriteEngine On
RewriteRule ^/?download/file_[0-9]+\.zip /download/file.zip [L]
If you are using htaccess files, you can place this rule in an htaccess file in the "download" folder:
RewriteEngine On
RewriteBase /download/
RewriteRule ^file_[0-9]+\.zip file.zip [L]

Apache - rewrite images to php file with .htaccess

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

How can I add an additional word to a URL using mod_rewrite

I have an application with URLs such as these:
https://www.domain.com/example/public/subscription
https://www.domain.com/example/public/subscription/source
https://www.domain.com/example/public/test/subscription
I'd like to use mod_rewrite (or some other method) to create shorter "aliases" of the above URLs by removing the /public/ part so that I can provide my client with these shorter versions of the URLs:
https://www.domain.com/example/subscription
https://www.domain.com/example/subscription/source
https://www.domain.com/example/test/subscription
In other words, when browsing to https://www.domain.com/example/subscription, for example:
The server must send back the same response that one would get when opening https://www.domain.com/example/public/subscription directly
The browser must still display the shorter version of the URL (without the /public/) in the address bar
Is this even possible, and how would such a RewriteRule look like?
Thank you in advance.
put this is .htaccess file in your DocumentRoot.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(example)/(.*)$ $1/public/$2 [NC,L]
You just want to "remove" the public keyword?
Try this rewrite rule:
RewriteRule ^/example/public/(.*) /example/$1 [L]

How to catch all characters with slashes after domain address with mod_rewrite as one parameter?

How to catch all characters after domain name as one parameter with mod_rewrite?
I have script which load other page content and displays that like standard page.
example script address look like that:
example.com/script.php/?www='/some/paths/etc'
I want to mask that URL to look like this:
example.com/some/paths/etc
If no paths exist it should look like
example.com/
There can be different amount of URL parts (a/b/c/.../X/)
I red about mod_proxy but I can do it with only .htaccess so it doesn't solve my problem.
I can't redirect one address to other, they have to be separated.
You can proxy via mod_rewrite (as long as mod_proxy has been compiled into the server), and this can be done via .htaccess files. Note that RewriteEngine On requires Options FollowSymLinks to be overridable in your .htaccess.
The following in your .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/script.php/?www=
RewriteRule ^/script.php/?www='([^']+)' http://example.com/$1
RewriteRule ^/(.*) /script.php/?www='$1' [P]
The RewriteCond matches your script path with a ?www= argument (if it fails to match no rewrite will take place). The first RewriteRule will match the www= argument and rewrite the visible path of the request. The second RewriteRule will then proxy the request (due to the [P] flag) to the script and will get the "content".
Note: this has not been tested, but hopefully will get you heading in the right direction to solve the problem. Also if script.php expects optional args &var2=whatever you will probably need to tweak the RewriteCond to match that and store it using parentheses. You can then recall those args using %1. More details can be found in the documentation.
Also to help you debug your rewriting you can use:
RewriteLog /full/path/to/your/log/file
RewriteLogLevel 3
EDIT: Added the RewriteLog stuff an clarified that the snippet at the top is to be put in the .htaccess file