Regular expression for referrer redirection - regex

I need the regular expression to redirect a link like this:
www.mysite.com/forums/users/tom
To: http://www.mysite.com/dashboard/listings/tom
The problem is that the script I am using constructs links like this:
www.mysite.com/forums/users/tom/favorites/
And I need to redirect ONLY the www.mysite.com/forums/users/tom and NOT the www.mysite.com/forums/users/tom/favorites/ which I would like to leave as it is.
Is there any way to do this?
Please help me. :(

This should work and only redirect mysite.com/referrer/ and not mysite.com/referrer/topic:
RewriteEngine on
RewriteRule ^referrer/$ http://someurl.com [L,R=301]
EDIT:
RewriteEngine on
RewriteRule ^forums/users/(.*)/?$ http://www.mysite.com/dashboard/listings/$1 [L,R=301]

Related

Remove a part of the URL using REGEX and .htacess

I want to replace this URL:
mydomain.com/posts/1659-artigos/etc-to
By this one:
mydomain.com/etc-to
Using .htaccess I'm trying the following:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ $1
But it isn't working. No redirect happens.
Can anyone help?
Thanks!
Converting my comment to answer so that solution is easy to find for future visitors.
You can use this code to get redirect working:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ /$1 [L,R=301,NC]
You need to use / before $1 for external redirect and make sure to use R flag for full redirect.

htaccess mod_rewrite expression for specific url structure

I have a URL-structure like this:
mypage.com/search/f7_987_Aprilia/f8_1234_Aprilia+RS+125
mypage.com/search/f7_987_Aprilia
mypage.com/search/f8_1234_Aprilia+RS+125
And I need them to route internally (without redirection) to the following structure:
mypage.com/search?&f[7]=987&f[8]=1234
mypage.com/search?&f[7]=987
mypage.com/search?&f[8]=1234
I have come this far: /(f\d+)(\d+).*/
This gives me the desired IDs in the first set. But how do I get the parameters separated by slashes? And how do I "convert" to the desired structure?
Is that possible with htaccess?
I hope there is a genius out there who can come up with the desired mod_rewrite regex
Thank you in advance!
You may use this code in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3 [L,QSA,NC]
RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3&$4[$5]=$6 [L,QSA,NC]

Apache Rewrite Regex Trouble

Struggling with a rewrite rule, think it is probably just a bit of incorrect syntax.
I have a URL like this:
http://mydomain.org/foo-bar/from2012-10-29/to2012-11-02/page2/
.htaccess is located within foo-bar/
My rules:
# if uri contains dates and page:
RewriteCond %{REQUEST_URI} "from.*to.*page" [NC]
RewriteRule ^from((19|20|21)\d\d-(0[0-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))/to((19|20|21)\d\d-(0[0-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))/page([0-9]+)/?$ index.php?from=$1&to=$2&p=$3 [NC,L]
As you can (hopefully) see, I am trying to translate this:
http://mydomain.org/foo-bar/from2012-10-29/to2012-11-02/page2/
into this:
http://mydomain.org/foo-bar/index.php?$from=2012-10-29&$to=2012-11-02&$p=2
But the variable values I am getting are not as I expect. I am getting the following values
p= 10
from= 2012-10-29
to= 20
Hope someone can point out the obvious for me.
Thanks in advance.
Simplify your regex.
Try this code:
RewriteRule ^from([^/]+)/to([^/]+)/page([0-9]+)/?$ index.php?from=$1&to=$2&p=$3 [NC,L,QSA]

URL Rewriting in .htaccess - getting confused with params

I searched in the web to find a valid solution for my problem, but nothing works.
Hopefully you guys can help me.
I want to Rewrite a URL on an Apache doing like that:
(1.) www.example.com/en/rainbow.html => www.example.com/index.php?site=rainbow&lang=en
or
(2.) www.example.com/rainbow.html =>www.example.com/index.php?site=rainbow&lang=
or
(3.) www.example.com//rainbow.html =>www.example.com/index.php?site=rainbow&lang=
To be honest, my understanding for regex isn't that good.
Tried:
RewriteEngine On
RewriteRule ^(.+)[/](.+).html|(.+).html$ index.php/?lang=$1&site=$2 [QSA]
Result: No Error, but "site" has only a param at (1.).
RewriteEngine On
RewriteRule ^(.*[/]){0,1}(.*).html$ index.php/?lang=$1&site=$2 [QSA]
Result: Works fine, but i.e. lang="en/", should be lang="en"
In which way I can improve it and let it work correct?!
Thanks a lot!!
Try this:
RewriteEngine On
RewriteRule (/en/)?([^/]+)\.html$ index.php/?site=$2lang=$1 [QSA]
No need to escape forward slashes in mod-rewrite, by the way. But you do need to escape the ., which, as you know, stands in as a wildcard in regular expressions.
I'm not an expert in url rewriting, but i like koalas:
RewriteRule /+(([^/]+)/)?([^/]+)\.html$ /index.php/?site=$3&lang=$2 [QSA]

Apache2: rewrite query string

I have the following url:
http://www.mysite.com/blog.php
and I want it to rewrite to:
http://www.mysite.com/index.php?goto=blog
How to do this in Apache2?
I've seen many examples of doing it the other way around...
Many thanks in advance,
If you want to rewrite all .php to index, you should do:
RewriteRule ^(.*).php$ index.php?goto=$1 [L,QSA]
if you would like to rewrite only some files:
RewriteRule ^(blog.php|other_file.php)/(.*)?$ index.php?goto=$1/$2 [L,QSA]
Try this (untested):
^/(.*)\.php$ /index.php?goto=$1
This should work even with other pages, not only with the blog.php.
Have a look at the documentation.