how to pass dynamic query string in SEO url - php

I'm using this query string URL for detail page "detail.php?id=860&c=house&t=engineer". It will redirect to "detail/860/house/engineer" using rewrite rule.
sometimes I have to pass an extra parameter in detail page but rewrite doesn't accept those query strings.
This is rewrite rule in htaccess
RewriteRule ^detail/([^/]+)/([^/]+)/([^/]+)$ detail.php?id=$1&c=$2&t=$3 [L,NC]
if I'd add the fourth param is 'rb' I can add here easily but the fourth param will be 'rb' or 'rul' or whatever may be.
how to do this?

You have to use it something like this if you want to use 5th parameter, detail/860/house/engineer/<dynamic param>/<your value>
RewriteRule ^detail/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ detail.php?id=$1&c=$2&t=$3&$4=$5 [L,NC]

Related

$_GET variable is not being set because of .htaccess file changes

I have been using '.htaccess' to rewrite how the page url should look so instead of:-
details.php?video=how+to+do+this&user=xxx
It should be more like this:-
/details/xxx/how+to+do+this
It's working and all, but here comes the issue; when I try to add a new $_GET category that wouldn't be useful all the time, that is the "page" get variable as not all video pages are going to have this variable. So when I add this variable nothing is set, it does show in the URL however.
/details/xxx/how+to+do+this?page=2
Here is the actual line of code that I used to rewrite one of the pages that's facing this issue.
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2
You can use:
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2 [NC,L,QSA]
QSA|qsappend When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
https://httpd.apache.org/docs/current/en/rewrite/flags.html

Can't get $_GET after Apache rewrite [duplicate]

I'm trying to rewrite an url from:
http://domain.com/aa/whatever/whatever.php
to
http://domain.com/whatever/whatever.php?language=aa
However, depending on existing $_GET variables, it either has to be ?language or &language.
To do this, I use 2 regexes with the [L] flag:
RewriteRule ^([a-z]{2})/(.*\.php\?.*) /$2&language=$1 [L]
RewriteRule ^([a-z]{2})/(.*) /$2?language=$1 [L]
The second one works as expected... The first one however is never hit (it falls through to the second regex, which does hit), even though Regex Coach does show me that it should.
edit:
If just read that I need to use two backslashes to escape the question mark. If I do this, it does hit on the first regex but never find the other GET variables.
From the documentation for mod_rewrite the pattern in RewriteRule matches against the part of the URL after the hostname and port, and before the query string so the query string is not included. That is why you don't get the other variables.
To add a new query string parameter language=xx whilst preserving any existing query string you need to use the QSA flag (query string append). With this flag, just one rule based on your second case should be sufficient:
RewriteRule ^([a-z]{2})/(.*) /$2?language=$1 [QSA]
You could setup the URL rewrite to pass the language to the php script via the PATH_INFO element of the $_SERVER superglobal. Just pass the language to the script like so:
foobar.php/en?args
In this case, $_SERVER[PATH_INFO] would equal /en

htaccess url rewriting, query parameters are not available in script

I am using following rule to rewrite a url in directory style,
RewriteRule ^post/([0-9]+)$ post.php?pid=$1
by using this i am directing localhost/post.php?pid=3 to localhost/post/3
but now I want to pass more parameters in default way like ?key=value.
for example localhost/post/3?comment_id=23
but this key value pair is not available in script.
When I do echo $_GET['comment_id'] it's not echo'ing anything.
How do I get it done.
You need to use QSA (query string append) flag. Change your rule to:
RewriteRule ^post/([0-9]+)$ post.php?pid=$1 [L,QSA]
QSA flag will append pid query parameter while preserving original query string in the URI thus you can do:
$_GET['comment_id']
and also:
$_GET['pid']

.htaccess with php hidden parameters and language selection

I can't seem to get my .htaccess file to route the urls to my site correctly. I have a number of languages people can choose from wanting URL's like:
http://www.domain.com/en/
http://www.domain.com/en/contact
But I can't seem to get the page 'contact' working when writing a rule to get the 'en' variable.
RewriteRule /([^/]+)/([0-9]+)/ index.php?language=$1
I use that to grab the language code but how could I get the contact page to work?
EDIT:
Apparently I needed some QSA option but now the language get variable grabs contact as the variable with the en
RewriteRule ^(.*)$ index.php?language=$1 [QSA,L]
With this rule the site:
http://www.domain.com/en/contact
Returns:
en/contact
EDIT2
What I am trying to accomplish is the directory structure:
/
/contact
/about
Having these folders in the root but grabbing and ignoring the /en/ language variable. So I don't need a second variable for &page=contact, I need it to route into the directory folder.
Try combining your two expressions, although you need to modify the second group - [0-9]+ will only match numbers, not words like contact. Try this:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?language=$1 [QSA,L]
The QSA option allows a query string to be appended to the clean URL, perhaps something like this:
http://www.domain.com/en/contact?to=support&subject=Hello
In response to your comment, this expression should do the trick:
RewriteRule ^([^/]*)/([^/]+)/?$ $2/index.php?language=$1 [QSA,L]
In the rewritten rule, $2 holds contact, for example, and $1 holds en. The former is used as the directory, and the latter as an argument in the query string.

HTAccess mod rewrite issue using QSA

I want to add a parameter to a URL but currently it isnt showing in the $_GET global.
A snippet from my htaccess file is as below:
RewriteRule ^account/blogs/([0-9]+)/([^\s?]+)/?$ /account/blog.php?blogId=$1 [L,QSA]
Then in my php code i want to add a link such as:
/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1
The wildcard (any char but space) option is for the blog title as i never know what it would be. I know want to add a query string param on the end such as ?delete=1. I however dont want this as part of the rewrite.
Does anybody know how to so this?
Thanks
My best bet would be to simply add get variables to the regex, like so:
RewriteRule ^account/blogs/([0-9]+)/([^\s?]+)/?(\?(.*))?$ /account/blog.php?blogId=$1&$4 [L,QSA]
This would rewrite
/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1
to
/account/blog.php?blogId=1&delete=1
It would also support additional variables.

Categories