Forum Moderators: phranque

Message Too Old, No Replies

url rewrite with multiple variable

get, htaccess

         

nugrohoredbuff

1:17 am on Sep 4, 2014 (gmt 0)

10+ Year Member



I have Apache 2.2.22with mod_rewrite enabled instaled on my debian wheezy pc. My PC processor is intel atom. I put my index.php on /home/myhomdir/www/mysite. I also make a symbolic link of the mysite directory to /var/www/mysite so I can call the site just using localhost/mysite.

I'd like to use url rewrite in .htaccess file to change link

localhost/mysite/?var1=1&var2=2&var3=3


into

localhost/mysite/1/2/3


could anybody suggest me the content of .htaccess file?

Thanks for help

ergophobe

2:54 am on Sep 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!

Lots of people around here are much better at this than I am, but off the top of my head....

I think you mean it the other way around - when the user puts 1/2/3/ into the address bar, it is rewritten to ?var1 etc

The regex is going to be something like this

RewriteRule ^mysite/(\d+)/(\d+)/(\d+)/?$ mysite?var1=$1&var2=$2&var3=$3 [L]

lucy24

7:51 am on Sep 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... with leading / in the target ;)

In localhost-type situations, targets always begin in / slash -- not only for rewrites (same as on live sites) but also for same-domain redirects if you've got any.

to change link

This wording makes me a little uneasy. There is nothing you can do in Apache to change a link. Those are created within the page; you have to either hard-code the desired link, or set up your php-or-equivalent to produce links in the form you want. All you can do in the server is look at incoming requests and then
(a) serve content from the requested directory and file
(b) rewrite to serve content from somewhere else on the same server
(c) redirect to tell the browser to make a fresh request

On a live site, you would probably need two rules:
one internal rewrite to take the pretty /1/2/ URL and fetch content from blahblah.php?foo=1&bar=2
and
one external redirect to take requests for blahblah.php?foo=1&bar=2 and force them to request /1/2/ instead.
But if it's a brand-new page with URL that nobody has seen before, you may be able to dispense with the external redirect.

Incidentally, this form
/mysite/?var1=1&var2=2&var3=3 

doesn't make much sense as a rewrite target. It will work, but you're forcing the server to take an extra step by invoking mod_dir. What you really want here is
/mysite/index.php?var1=1&var2=2&var3=3

Since it's an internal rewrite, nobody will ever see the "index.php" part.

ergophobe

3:55 pm on Sep 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



with leading / in the target


I always forget which way it goes. In a <Directory> context you don't use it, in other contexts you do.

index.php


Oops. Good catch.

lucy24

6:57 am on Sep 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In a <Directory> context you don't use it, in other contexts you do.

You're thinking of the pattern. Directory context (whether <Directory> section or htaccess): no leading slash in pattern. Loose in config: leading slash in pattern.

The form of the target is always the same. If you leave off the leading slash, it becomes a relative URL unless you have previously specified a RewriteBase and the rule is configured as an internal rewrite. I did some experimenting on this pretty recently, for some reason that now escapes me.

ergophobe

3:40 pm on Sep 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah... I wonder if I don't pay attention to that because my match is almost always root relative anyway, so in that case it doesn't actually change anything