Apache2: rewrite query string - regex

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.

Related

Redirect multiple category URLs to one category URL using Regex

Struggling to understand the Regex needed for this form reading several forum posts. Hoping someone who's an expert can provide a quick answer to this.
/shop/accompaniments/product-name-a/
/shop/bbq/product-name-b/
/shop/bundles/product-name-c/
All to redirect to
/product/product-name-a/
/product/product-name-b/
/product/product-name-c/
Thanks
Anthony
Maybe try something like:
RewriteRule ^(.*)shop/accompaniments(.*)$ /$1product$2 [R=301,L]
RewriteRule ^(.*)shop/bbq(.*)$ /$1product$2 [R=301,L]
RewriteRule ^(.*)shop/bundles(.*)$ /$1product$2 [R=301,L]

Pretty URLs using .htaccess

Everyone!
I have url like: https://example.com/web/web/publications/11233123-21331-233256667
How make this url: http://backend.example.com/web/web/publications/11233123-21331-233256667
Thanks. Sorry I am relatively new to .htaccess.
A simple .htaccess would look something like this:
RewriteEngine On
RewriteRule ^ http://backend.example.com%{REQUEST_URI}

Regular expression for referrer redirection

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]

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]