0

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.

2
  • You cannot access get parameters like that, since they are simply not part of the pattern you match your regex against. Please take a look at the excellent documentation of the rewriting module, this is explained in detail in there: httpd.apache.org/docs/current/mod/mod_rewrite.html Commented Feb 26, 2014 at 9:52
  • Thanks for the documentation link Commented Feb 27, 2014 at 2:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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']);

1 Comment

I tried this one exactly before. But the GET param isn't matched by the regex at all. Thanks for answering though =]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.