Mod rewrite pass multiple parameters without directories - .htaccess

I need to redirect https://www.example.com/test/track.gif?par1=1&par2=2&par3=3 to https://www.example.com/test/track.php?par1=1&par2=2&par3=3.
This is what I have so far:
RewriteRule ^test/track\.gif(.*)(.*)(.*)$ test/track.php?par1=$1&par2=$2&par3=$3 [NC,L]
Which doesn't work. If there was directories between it would be easy, but I am not sure how I can pass parameters without directories.

Just found answer:
RewriteRule ^test/track\.gif$ test/track.php [NC,L]
Now all parameters will be passed to track.php and can be accessed with $_GET variable.

Related

Redirecting URLs with multiple parameters to clean URLs in .htaccess

I'm attempting to redirect URLs in .htaccess that have multiple parameters for example:
http://www.example.com/index.php?p=product&id=10&parent=0
to a clean URL on another domain like
http://example2.com/product/product10/
I believe my normal method of redirection (using a PHP header in an index file of a directory I've created) will not work due to not being able to put ? in a directory name.
I've done some minor .htaccess but have no experience with escaping parameters or anything and the only tutorials I can find are for escaping only a single parameter.
Can anybody give me a few pointers please?
You can use that in your root .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=([^&]+)&id=([^&]+) [NC]
RewriteRule ^index\.php$ http://example2.com/%1/%1%2/? [R=302]
Change [R=302] for [R=301] when test work well.

htaccess rewrite all urls with a certain query to a parent directory

Sounds actually simple and at least solvable by researching. But unfortunately I don't get it done and need to ask for help.
I want to redirect all URLs which contain certain parameters to a parent directory. My solution only works if the url doesn't contain any additional parameter:
RewriteRule ^cars/mercedes-brands$ /mercedes? [R=301,NE,NC,L]
RewriteRule ^motorbikes/suzuki-brands$ /suzuki[R=301,NE,NC,L]
Redirect works for
http://www.example.com/cars/mercedes-brands ---> http://www.example.com/mercedes
http://www.example.com/motorbikes/suzuki-brands ---> http://www.example.com/suzuki
But not for
http://www.example.com/cars/mercedes-brands/test/123
http://www.example.com/motorbikes/suzuki/12?page=3
Any ideas? Thank you so much!
You need to specify there is (maybe) something after mercedes-brands and suzuki-brands.
Here are your 2 rules modified
RewriteRule ^cars/mercedes-brands(?:/.*)?$ /mercedes? [R=301,NE,NC,L]
RewriteRule ^motorbikes/suzuki-brands(?:/.*)?$ /suzuki? [R=301,NE,NC,L]

.htaccess url rewriting without collision

I've got the following code in my htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
The above code works fine like this, http://domain.com/username instead of http://domain.com/profile.php?username=username
I need a similar one but with a fake string like this
http://domain.com/gallery/username
The file is at http://domain.com/gallery.php
How do I achieve this without colliding with profile code?
Almost the same:
RewriteRule ^gallery/([a-zA-Z0-9_-]+)/?$ gallery.php?username=$1
If you also would allow other non ascii nicknames you should replace ([a-zA-Z0-9_-]+) by ([^\/]+).
Just as a hint you can remove the first rule if you add a questionmark after the slash that makes the slash optional.

Targeting single directory for rewrite rule

I have edited this question to use the actual URLs. I need the url
http://westernmininghistory.com/mine_db/main.php?page=mine_detail&dep_id=10257227
To be rewritten like
http://westernmininghistory.com/mine_detail/10257227/
I have tried
RewriteRule ^([^/]*)/([^/]*)/$ /mine_db/main.php?page=$1&dep_id=$2 [L]
Which works on this page but breaks every other page on the site. I was wondering if there was a way to force the rewriterule to only operate on files within the mine_db directory. I had tried RewriteCond but with no success:
#RewriteCond %{REQUEST_URI} ^/mine_db
I really don't know they proper syntax for this though. Any ideas?
First of your rule can be shortened and written without needing RewriteCond. Also it appears that you want to capture 2 variables after test_db.
You can try this rule instead:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC]
Which will work with URIs like /mine_detail/12345 (trailing slash is optional). Also note that above rewrite will happen silently (internally) without changing the URLi in browser. If you want to change URL in browser then use R flag as well like this:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC,R]

mod rewrite exclude all but php

Is it possible to edit htacces in such a way that only the following url is rewritten and the rest isn't?
http://www.example.com/index.php?x=foobar
to
http://www.example.com/foobar/
I want the pages not having x=... as a variable to behave normally
I got the following but that doesn't work
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/ index.php?x=$1
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$
Who can help me?
First off, the RewriteCond must be put before the RewriteRule to which it belongs.
But I think that you need another approach for your case, something like this:
RewriteRule (.*)\.php - [PT,L]
RewriteRule ^(.*)/$ index.php?x=$1
The first rule Passes Through (PT) every PHP page, so the second rule is only applied to all non-PHP requests.
That second rule only applies to a "simple path", no matter if this path has a dot in it or not (e.g. hello.gif/ will match, too).
If this does not work for you, then you might consider one of these points to start further research:
the pattern ([^\.]*) matches everything that does not have a dot in it
see RewriteCond to skip rule if file or directory exists for RewriteConds where the following RewriteRule is only used if the request does not point to an existing file or directory
Hope this helps.

Resources