How to add an optional "param" in a regular expression in .htaccess - php

I've following 2 rules in my .htaccess file -
1. RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
2. RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [L]
so that visitng /myscript/d sends a request as /myscript.php?action=check&var=d
I am trying to add an option page parameter so that visiting /myscript/d?page=5 sends the requests as /myscript.php?action=check&var=d&page=5
to achieve this I tried this
RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
But this rules is being ignored and the request is sent as /myscript.php?action=check&var=d (i.e. 2nd rule from the above is being applied). What am I doing wrong here? What changes do I need to make it get it working?
Thanks for your help.

Use 2nd rule only and add QSA flag:
RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [QSA,L]
This will pass existing query parameter to the new URL:
/myscript/d?page=5&say=hello
=>
/myscript.php?action=check&var=d&page=5&say=hello
Apache documentation: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

Related

I want to make my long reference link dynamic with htaccess

First of all, I have a link that takes 2 parameters;
http://localhost/project/home.php?SK=2&referance=1
?SK=2 is my first parameter and &referance=1 is my second parameter. The SK=2 parameter draws my files in the php file, and the referance=1 parameter prints the value from the get method to my registration page. What I want is to put this link in below format with htaccess
http://localhost/project/register?referance=1
The link I made before brings my register page, but now I want it to bring the reference code, but unfortunately it doesn't.
Htaccess code I used before;
RewriteRule ^register$ home.php?SK=2 [NC,L]
RewriteRule ^register$ home.php?SK=2 [NC,L]
You just need to add the QSA (Query String Append) flag to your existing rule.
For example:
RewriteRule ^register$ home.php?SK=2 [QSA,NC,L]
This will now rewrite /register to /home.php?SK=2 and /register?referance=1 to /home.php?SH=2&referance=1.
Reference:
http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

RewriteRule of subdirectory gets ignored, L present

I'm out of solutions here after over a week of tries and errors.
I'm looking to add a GET parameter to an URL as a rewrite, if the actual rewrite contains an additional value. For example: /foo should redirtect to index.php?p=test1 and /foo/barshould add a second GET argument, let's name it test so it gets index.php?p=test1&test=something.
My current not working set if the following:
RewriteRule ^/?acadie/lisette/?$ index.php?p=contact_single&id=531&ID_AGENT=1380 [QSA,L,S=1]
RewriteRule ^/?acadie/?$ index.php?p=contact_single&id=531 [QSA,L]
If I add RewriteRule .* - [F] before the second statement, it successfully got 403, so I assume my rule is just ignored at all. If I reverse them, same.
What I'm doing wrong?
I'm running Apache 2.4.29

How to set .htaccess for 2 type get value on the same page?

For this page test.php
i have to use 2 format url
www.example.com/test/1234
and
www.example.com/test/1234/abcd
My .htaccess is
RewriteRule ^test/([^-]*)/([^-]*)$ /test.php?one=$1&two=$2 [L]
It's work good with www.example.com/test/1234/abcd but redirect internal error with www.example.com/test/1234
I want to know how can i edit .htaccess for work good with my 2 url format on test.php ?
Try the following instead:
RewriteRule ^test/([^-]*)$ /test.php?one=$1&two=$2 [L]
Just remove the 2nd /([^-]*) it is making no difference, as you can see here. Removing it will allow for you to access www.example.com/test/1234 too.
You can use:
RewriteRule ^test/([^/-]+)(?:/([^-]*))?$ /test.php?one=$1&two=$2 [L]

.htaccess writerule to remove question marks and equal mark

today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.

How to get values from query string and pass it to new page using .htaccess file

currently I m using following code in my site in .htaccess file :
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L]
This redirects user to comman.php page say, user requests
http://www.mysite.com/myfolder/page1.php
will redirects to
http://www.mysite.com/myfolder/comman.php?cat=page1
This works fine. My question is how can I achieve following
http://www.mysite.com/myfolder/page1.php?var1=123
to redirect
http://www.mysite.com/myfolder/comman.php?cat=page1&param=123
i.e. whatever value passed to url using get method to add in my new url.
Thanks in Advance.....
You should add the QSA flag:
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L,QSA]
QSA stands for Query String Append. Anything after the ? in the original URL will be appended to the rewritten URL.
You need to ass QSA to your rule, i.e.
RewriteRule ^(.*)$ /index.php?pageid=$1 [QSA,L]
You can use the QSA flag in your RewriteRule for that.
See the docs.

Categories