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

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.

Related

Using mod_rewrite with chinese characters in Apache

I am having trouble finding information on Apache mod_rewriting using Chinese characters (all the info I can find relates to numbers).
I want to rewrite /character.php?character=宠 (where the character is the result of a search and thus will vary) to /character/宠.
This is my (poor) attempt:
RewriteRule ^character/?$ characters?character=$1 [NC,L]
I would appreciate any help.
Thanks,
Dan
First of all, your Regular Expression is incorrect. Using the ? tells mod_rewrite that the character before it is optional. It is not a placeholder for any character.
You should be doing this instead:
RewriteRule ^character/(%[A-Z0-9]{3})$ characters?character=$1 [NC,L]
This rule assumes you want to only capture one character. If this is not the case, or you need the same rule elsewhere, then swap out (%[A-Z0-9]{3}) for (%[A-Z0-9]+).
You also need to make sure that your .htaccess file is saved in Unicode format (UTF-8).
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 ^character/(.+)$ /characters?character=$1 [NE,NC,L,QSA]

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]

Apache mod_rewrite help?

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.

Apache mod_rewrite redirect all requests for files not named x to this domain

Ok, I'm trying to achieve this:
Redirect all files of a particular filetype (except particular exceptions) to be loaded from another server, right now I'm using this rewrite rule:
RewriteEngine on
RewriteRule \.(swf|jpg|png)$ http://example.com%{REQUEST_URI} [NC,R=302,L]
And it will redirect all the swf,jpg, or png files to another hostname to serve them.
So how can I add exceptions to this rule for specific files? I've tried adding:
RewriteCond %{REQUEST_URI} ^!(log22.swf|packer.swf|sos.swf|aeVisual.swf|newBubbleSystem.swf|e.swf)$
But when I did this it completely stopped the rule from doing anything at all to the other files. Any ideas? I'm a newbie at using mod_rewrite.
You have to preprend ! to the condition's regex:
RewriteCond %{REQUEST_URI} !^(log22.swf|packer.swf|sos.swf|aeVisual.swf|newBubbleSystem.swf|e.swf)$
Note that ! and ^ are in different order than in your code, as you can negate the expression, but adding the ! before.
The reason the rule does not work is that it does never match.

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