Apache .htaccess regex for GET param - php

I have a similar rule in my .htaccess
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ /?name=$4&year=$1&monthnum=$2&day=$3
Example
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/
Now I am required to add in extra parameter at the back such that it looks like
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/?source=email
I wish to get the source value and I tried the following:
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/([^/]*)/\?source=(.*)$ /?name=$4&year=$1&monthnum=$2&day=$3&source=$5
but it's not working. Any advice?
Thank you.

I haven't tried this myself, can you please give it a try
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ /?name=$4&year=$1&monthnum=$2&day=$3 [QSA]
The [QSA] Query String Append flag will retain the existing query strings. So your query string
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/?source=email
Should get converted to
/?source=email&name=$4&year=$1&monthnum=$2&day=$3
Please give it a try.

Try
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/([^/]*)/\?$ /?name=$4&year=$1&monthnum=$2&day=$3&source=$5
When you get source value..... just use
$SrcVal = str_replace('source=','',$_GET['source']);

Related

I want to remove query string variable and replace it query value in php

$url = example.com/your-new-key-is.php?key=5&submit=success
The above url consists query string and submit value, also it's not user friendly
Once I click the submit the new url want to be like that
$url = example.com/your-new-key-is-5/
I want to replace the key url with value(5) and truncate the part followed & symbol.
Anybody knows the answer ?
I think you can:
$url1 = example.com/your-new-key-is.php?key=5&submit=success
$url2 = example.com/your-new-key-is-5/
<?php
$url= str_replace($url1 , $url2);
?>
You can see more examples here: function.str-replace.php
You need to use mod rewrite. There are ways to write them. You can also use a rewrite rule generator to understand how its done.
Below are the rules that needs to go into the .htaccess file
# Rewrite --- example.com/your-new-key-is.php?key=5&submit=success => example.com/your-new-key-is-5/
RewriteCond %{QUERY_STRING} (^|&)key=5($|&)
RewriteCond %{QUERY_STRING} (^|&)submit=success($|&)
RewriteRule ^example\.com/your-new-key-is\.php$ /example.com/your-new-key-is-5/?&%{QUERY_STRING}
Here is a link to the Generator.
http://www.visiospark.com/mod-rewrite-rule-generator/
After you've written the rule to htaccess, you will enter in example.com/your-new-key-is-5/ which is equivalent to example.com/your-new-key-is.php?key=5&submit=success

.htaccess slash in name get variable issue

Hi and sorry for bothering you, i try to look around but i couldn't find a solution, and i really can't figure out how and if it's possible to do something like that with rewriterule.
This is the portion of htaccess file interested in my problem:
RewriteRule ^pagename/([^/]+)-([^/]+) mypage.php?name=$1&id=$2 [NC,L]
example.com/mypage/dogarticle-800ad43
so my variable in php will be like
$_GET["name"]=dogarticle and $_GET["name"]=800ad43
so far so good, but if an article contain a slash i can't get the right variable anymore, example
example.com/mypage/animals/dogandcat-800ad44
where animals/dogandcat should be name i receive the following get vars:
$_GET["name"]=animals $_GET["name"]=dogandcat
Here the question what should i do to receive the following get var:
$_GET["name"]=animals/dogandcat $_GET["name"]=800ad44>
Cant really figure out the solution, probably not the best of my days.
Thanks
edit:
in the name there can be a lot of slash example:
example.com/mypage/animals/dogandcat/squirrel/someotheranimal-800ad44
You can use:
RewriteRule ^pagename/(.+)-([^/]+)$ mypage.php?name=$1&id=$2 [NC,L]

PHP `$_GET` variables without the `?id=` [duplicate]

I know this is probably a common question and has been asked before but .htacess looks foreign to me and I can't seem to get this to work from looking at previous questions.
I have a index script that searches and the syntax is:
.../listing/?field=property_type&query=retail
I simply want to rewrite this to:
.../listing/{field}/{query}
One other catch, I have some fields that are boolean.. for example:
.../listings/?forlease=1
So in this case I just use isset() but would like to rewrite this to:
.../listings/forlease/
Thanks for your help as always! Stack Overflow rocks!
EDIT:
I see some good answers but completely what I'm looking for:
User requests: .../listing/property_type/retail/
Apache rewrites to: ../listing/?field=property_type&query=retail
Script sees: $_GET['field']='property_type' & $GET['query']='retail'
In the case of the boolean (all booleans will have only one Dir):
User requests: .../listing/forlease/
Apache rewrites to: ../listing/?forlease=1
Script sees: $_GET['forlease'] = 1
If your webserver is Apache, then you can use mod_rewrite.
Check out : http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html =)
In your .htaccess you can have a couple of simple rules:
RewriteRule ^.*/listings/([a-zA-Z0-9_]+)/([a-zA-z0-9_]+)$ /listing/?property_type=$1&query=$2 [QSA,L]
RewriteRule ^.*/listings/forlease/$ /listing/?forlease=1 [QSA,L]
RewriteRule ^.*/listing/([^/]*)/?$ listing/?$1=1 [NC,L]
RewriteRule ^.*/listing/([^/]*)/?(.*)$ listing/?property_type=$1&query=$2 [NC,L]
Try this, first checks for a single condition (http://...listing/forlease), otherwise rewrites to (http://...listing/field/query)
You might want:
RewriteRule ^.*/listing/([^/]*)/?$ listing/?$1=1&%{QUERY_STRING} [NC,L]
RewriteRule ^.*/listing/([^/]*)/?(.*)$ listing/?property_type=$1&query=$2&%{QUERY_STRING} [NC,L]
To keep any additional parameters in the query string.

rewrite url and let more parameters

I am rewriting this kind of urls
http://deia.info/category/calendar/10/
To
/index.php?task=browse_posts&catid=$2
By this code in the .htaccess
RewriteRule ^category/(.+)/(.+) /index.php?task=browse_posts&catid=$2
Which seems to do the job. The problem is when additional parameters are sent, those are ignored.
For example:
http://deia.info/category/calendar/10/?start_date=10
isset($_GET['start_date']) won't return true
Why?
When the incoming URL holds a query like in the OP example:
http://deia.info/category/calendar/10/?start_date=10
The way to combine (Append) it with/to a new query in the substitution URL, is by adding the QSA Flag.
So add [L,QSA], like this:
RewriteRule ^category/(.+)/(.+) /index.php?task=browse_posts&catid=$2 [L,QSA]

get variables in query string re_writes

I am trying to combine these four get variables (sch, val, lmt, ord) into a nice looking url re_write. Currently I have the following in my .htaccess file and it works if all variables are included, however, if I only input the first two variables (sch & val) it breaks:
RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$
search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]
www.domain.com/search/City/London/5/asc works
www.domain.com/search/City/London but this doesn't
Why not just add the other rewrite rules ahead of it. IE:
RewriteRule ^search/([^/]*)$
search.php?sch=$1 [L]
RewriteRule ^search/([^/]*)/([^/]*)$
search.php?sch=$1&val=$2 [L]
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$
search.php?sch=$1&val=$2&lmt=$3 [L]
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$
search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]
That should work. Not sure if it is what you want, but I do not really think it is "possible" to do that without just doing a query and passing the full query string as one line then parsing it PHP side.
EDIT
The nice thing about doing all this in .htaccess vs PHP is it requires less logic and you pass what you want to your script in the correct values. My preference would be using the .htaccess above.
you can use this , to grab whole path as text and parse
www.domain.com/search/City/London/5/asc works www.domain.com/search/City/London but this doesn't
RewriteRule ^search/(.*)$ search.php?url=$1
$paths=explode('/',$_GET('url'));
output
www.domain.com/search/City/London/5/asc
$paths=city,london,5,asc
www.domain.com/search/City/London
$paths=city,london
You can use loop and pair as key/value... if needed

Categories