Mod Rewrite URL with Number - regex

I want to rewrite an url from example.php?id=123 to example-123-hello-world
(where "hello-world" is some fake text)
I tried this:
RewriteRule ^example-(.*)-(.*)$ /example.php?id=$1
it works only if there are no "-" in hello world:
example-123-helloworld
Any ideas how to do this? thanks a lot! :)

here's what you need to use:
RewriteEngine On
RewriteBase /
RewriteRule ^example\-([0-9]+)\-(.*)/?$ example.php?id=$1 [NC,QSA,L]

Try this:
RewriteRule ^example-(.*?)-(.*)$ /example.php?id=$1

Related

How to replace space in uri with Isapi rewrite

i have to replace spaces in uri like :
/parma/vendita/villa%20schiera/via%20cremonese/
With a - char to have this:
/parma/vendita/villa-schiera/via-cremonese/10339
I really appreciate a suggestion to write isapi rewrite rule
Thanks :)
Try this rule:
RewriteBase /
RewriteRule ^(.*)\%20(.*)$ /$1-$2 [NC,LP]

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]

Regular expression transcription

I have to translate this: http://*myurl*/feedback.php?rcode=1307954819&lang=it
in
http://*myurl*/index.php?option=com_cake&module=lodgings&task=feedback&id=1307954819
Can someone help me ? :)
Edit:
I have to write it in .htaccess so I need rewrite rules.
# activate rewrite engine
RewriteEngine On
# mark / as a root
RewriteBase /
# rewrite feedback.php
RewriteCond %{QUERY_STRING} ^rcode=(\d+)
RewriteRule ^feedback.php$ index.php?option=com_cake&module=lodgings&task=feedback&id=%1 [L]
The rule above will rewrite /feedback.php?rcode=1307954819&lang=it into /index.php?option=com_cake&module=lodgings&task=feedback&id=1307954819 without changing URL in address bar of the browser.
If you need to change URL in address bar as well (to make a redirect) then change [L] into [R=301,L]
You don't need RegEx for this.
You can use str_replace.
$URL='http://myurl/feedback.php?rcode=1307954819&lang=it';
$newURL=str_replace(array('http://myurl/feedback.php','?rcode=','&lang=it'),array('http://myurl/index.php','?option=com_cake&module=lodgings&task=feedback&id=',''),$URL);
Without taking into acount option amd module params:
$url = 'http://*myurl*/feedback.php?rcode=1307954819&lang=it';
echo preg_replace('%^http://([^/]*)/([^.]*)\.php\?rcode=([0-9]*).*$%','http://$1/index.php?option=com_cake&module=lodgings&task=$2&id=$3',$url);
In .htaccess it should be:
RewriteRule ^/([^.]*)\.php\?rcode=([0-9]*).*$ /index.php?option=com_cake&module=lodgings&task=$1&id=$2
My example is in C# (not a php developer), but the regex pattern should work in both languages..
void Main()
{
string input = #"http://myurl/feedback.php?rcode=1307954819&lang=it";
Regex pattern = new Regex(".+?&?rcode=([^&]+)");
string output = pattern.Replace(input, #"http://myurl/index.php?option=com_cake&module=lodgings&task=feedback&id=$1");
Console.WriteLine(output);
}

Mod_rewrite is escaping my query strings!

I have this url:
http://dev.example.com//zina/Artist%20One%20Demo?l=7&img=artist%20pic%20can%20can%20be%20named%20anything.jpg&it=full
And it's being rewritten to this:
http://dev.example.com/?p=Artist%20One%20Demo&option=com_zina&Itemid=82&l=7&img=artist%2520pic%2520can%2520can%2520be%2520named%2520anything.jpg&it=full
Because of this rewrite rule:
RewriteRule ^zina/(.*)$ /?p=$1&option=com_zina&Itemid=82 [L,R,QSA]
Does anyone know why, and how I can correct it?
Try the NE flag:
RewriteRule ^zina/(.*)$ /?p=$1&option=com_zina&Itemid=82 [L,R,QSA,NE]