Rewriting Parameters to Subfolder Structure (htaccess) - .htaccess

I am looking to rewrite 2 parameters to form a cleaner structure:
CURRENT: ?fruit=Oranges&vegetable=Carrots
DESIRED: /oranges-vs-carrots/
Any comments or explanation on the regex would be really appreciated.

Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} \?fruit=([^&]+)&vegitable=([^\s]+) [NC]
RewriteRule ^ /%1-vs-%2? [NC,R,L]
RewriteRule ^([^-]+)-([^/]+)/?$ /?fruit=$1&vegitable=$2 [NC,L]

RewriteRule ^([a-z]+)-vs-([a-z]+)/$ ?fruit=$1&vegetable=$2
site for testing it: http://martinmelin.se/rewrite-rule-tester/

Related

Redirect URL with GET variable to site with the GET variable in it

I would like to redirect, for example :
index.php?name=sarah
to
names/sarah.php
The folder structure:
names/...
index.php
I've tried :
RewriteRule index.php?name=$1 /names/$1.php
and other ones, but didn't came to a solution.
Is this possible? If so, how can you do this?
Note:
The other way round is
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
which works fine.
Here is your rule:
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?
You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]

.htaccess - Rewrite query string and redirect to directory

I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>

change the url using with htaccess

i am trying to change the url using htaccess rewrite
i want to change this url
page/details.php?name=abcdef&id=18
to
page/abcdef
Here my sample code for this
RewriteEngine On
RewriteRule ^company/([A-Za-z0-9-]+)/$1 company/details.php?name=$1&id=$2 [R=301,L]
this is not working, and also i was tried many code but not working,please help me to find htaccess code for this url
Thanks advance
This should do what you're after:
RewriteCond %{QUERY_STRING} ^.*name=([a-zA-Z0-9]*).*$
RewriteRule ^page/(.*)$ page/%1? [R=301,L]
Try this one:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^name=(.*)&id=(.*)$
RewriteRule ^page/(.*)$ /page/%1? [R=301,L]
This will check for these 2 parameters in the query string.

mod_rewrite do not change links when rewrite

I have the following to my .htaccess
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
When I call www.mydomain.com/page/myvalue I get the correct result. In order to to that though I'll need to change all links to my website correct?
Is there a way to accomplish the opposite i.e. when I call index.php?page=$1 to redirect me to www.mydomain.com/page/myvalue ?
Add this rule to your .htaccess:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=301,L]

How to use mod_rewrite

I have a site (sports.com, let's say) and I'd like to set up mod_rewrite so that a visitor to "football.sports.com/america" is shown content from "sports.com/stuff/place.php?id=america". I'd still like visitors to the naked "sports.com" to see "stuff/index.php".
Sorry, I'm a real newb with this. I've tried and failed several times, heres what I have so far...
IndexIgnore *
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} football\.sports\.com [NC]
RewriteRule ^([^/.]+)$ stuff/place.php?id=$1 [L]
RewriteRule (.*) stuff/index.php?%{QUERY_STRING}
Any help would be massively appreciated.
You can do this like:
RewriteRule ^america$ america/ [NC]
RewriteRule ^america/$ stuff/place.php?id=america [NC,L]
Redirects football.sports.com/america[/] to football.sports.com/stuff/place.php?id=america. This is a static rule: it won't work with france for example.
If you need a generic way you need :
RewriteRule ^([^/.]+)$ $1/ [NC]
RewriteRule ^(.*)/$ stuff/place.php?id=$1 [NC,L]
To get naked site to be redirected, add:
RewriteRule ^$ stuff/index.php [L]
Redirects football.sports.com/ to football.sports.com/stuff/index.php
Eventually you can add the query string of necessary.

Resources