optional parameter in url rewriting - .htaccess

I'm using apache and .htacess to rewrite my urls.
I would like to have an optional parameter "mess" in my ad.php page. I wrote :
RewriteRule ^ad-([A-Za-z0-9-]+)/?$ ad.php?id=$1 [NC,L]
RewriteRule ^ad-([A-Za-z0-9-]+)-([A-Za-z0-9-]+)/?$ ad.php?id=$1&mess=$2 [NC,L]
But it seems that only the first rule is considered. ad-100 and ad-100-1 should give different things but they don't. When I remove The first Rule, ad-100 is not working anymore (obviously) and ad-100-1 is now working because it's taking the second rule.
Do you know how I can have optional parameters ? Should I combine the two rule in one ?

This should work:
RewriteCond %{REQUEST_URI} !ad\.php [NC]
RewriteRule ^ad-([^-]+)/?$ ad.php?id=$1 [NC,L]
RewriteCond %{REQUEST_URI} !ad\.php [NC]
RewriteRule ^ad-([^-]+)-([^/]+)/? ad.php?id=$1&mess=$2 [NC,L]
Optionally, you could use one rule for both parameters if there is no problem having mess value empty when there is only one parameter. Like this:
RewriteCond %{REQUEST_URI} !ad\.php [NC]
RewriteRule ^ad-([^-]+)-?([^/]+)?/? ad.php?id=$1&mess=$2 [NC,L]

Related

.htaccess ModRewrite with 2 parameters

#anubhava provided an excellent answer for my previous question of doing an .htaccess internal rewrite with the below code, which worked for my one search query.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
I wanted to make this a separate question since my next question is slightly different. How could I adapt this to also work with two parameters? For instance, I would also like http://ipaddress/directory/file.php?id=47?name=value1 to redirect to http://ipaddress/directory/47/value1
name= can also be any combination of letters and numbers, like value1050, etc.
Thank you #anubhava for your previous answer above, and maybe there's a way to add on this second parameter as well?
Considering you are segregating your query string values in id=1234&name=value123 style, since passing 2 times query string will not be allowed, then you could try following, fix of your shown attempt.
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=(.*)$ [NC]
RewriteRule ^file\.php/?$ /directory/%1/%2? [R=301,L,NC]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [L,QSA,NC]
2nd solution: Adding 1 more solution here, either use above OR use following one at a time only please.
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?d=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]

ReWrite rule not working with two parametrs

im quite new to rewrite rules.
I can manage with one variable and thats it.
I have webpage Where the rewriterule is:
RewriteCond %{HTTP_HOST} ^www\.someserver\.com$
RewriteRule ^/?$ "http\:\/\/someserver\.com\/" [R=301,L]
RewriteRule ^(\d+)*$ ./index.php?comp=$1
RewriteRule ^(\d+)*/$ ./index.php?comp=$1
And it all work fine as it should. But now as i want 1 more variable into URL
i cant get it to work.
Right now the $1 is only numbers.
someserver.com/1554886
But i want two variable.
someserver.com/1554886-SOMENAME-WHATEVER-WORD-AND-HOW-MANY
But it wont show.
i tried smth like this:
RewriteRule ^([^-]*)-([^-]*)$ ./index.php?comp=$1&string=$2 [L]
How do i get it to work?
Do i have to make some changes in the php side as well?
everything what comes after the number part of the URL is there only for
SEO, the number is Unique
You need one more rule to handle two parameters:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(someserver\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(\d+)/?$ index.php?comp=$1 [L,QSA]
RewriteRule ^(\d+)-(.+)/?$ index.php?comp=$1&string=$2 [L,QSA]

Apache Rewrite with variables in Query String

I have a two part problem that I have only been successful with the first part.
I have the following listed in .htaccess which works great:
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.
My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.
My attempt so far has been:
RewriteCond %{QUERY_STRING} ^dID=1$
RewriteRule ^demo.php$ senior [NC]
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
Thanks for your time and help.
You want to match against the request instead of the query string and redirect the browser:
RewriteCond %{THE_REQUEST} \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$ /senior? [NC,R]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Place this additional rule before your current rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.

htaccess rewriterule with ? arguments?

What am I doing wrong, here?
I've moved to a new Web store platform (OSCommerce to Drual/Ubercart). OSCommerce uses arguments to pick out products.
I want to redirect from this:
http://www.ztwistbooks.com/oscstore/product_info.php?products_id=64
To this:
http://www.ztwistbooks.com/node/39
This does NOT work (it gives a 404):
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^oscstore/product_info.php\?products_id\=64$ "http\://www.ztwistbooks.com/node/39" [R=302,L]
Other rewrite rules are fine, for instance, I can redirect stuff that doesn't use the ?arg=value, and it works fine:
RewriteCond %{HTTP_HOST} ^.$
RewriteRule ^oscstore/?(.)$ "http\://www.ztwistbooks.com/index.php" [R=301,L]
RewriteRule's pattern is applied to the URL-path, so you need something like this:
RewriteCond %{QUERY_STRING} ^products_id=64$
RewriteRule ^oscstore/product_info.php$ http://www.ztwistbooks.com/node/39 [R=302,L]

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources