Trying to create a rewrite rule in .htaccess - .htaccess

I have a url that looks like this
www.myurl.com/page/blog
which works fine with my current rewrite rule. But when I try to append append additional query stings to it it doesn't work.
www.myurl.com/page/blog?group=2
Is there something I could add to my rewrite rule that will make it catch all additional query strings?
This is my current rule:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]

You need to add the [QSA] flag to pass through the original query string params i.e.
RewriteRule ^page/([^/.]+)/?$ index.php?page=$1 [L,QSA]

Related

Apache mod-rewrite with get parameters

I now have this two rules which work ok
RewriteRule ^paymentok$ ?,return,payment&status=OK
RewriteRule ^paymentfail$ ?,return,payment&status=FAIL
What i need is also the get parameters after the paymentok or paymentfail, i.e. when this page is accessed:
paymentok?myvar1=dfsdf&myvar2=ewrwe
then the redirected URL should be:
?,return,payment&status=OK&myvar1=dfsdf&myvar2=ewrwe
Is this possible with rewrite rules?
YOu need to invlude the QSA flag in your rules:
RewriteRule ^paymentok$ ?,return,payment&status=OK [L,QSA]
RewriteRule ^paymentfail$ ?,return,payment&status=FAIL [L,QSA]
The QSA flag (Query String Append) appends any existing query string to the new one that you've created in your rule's target.

htacces rewrite - extend url without modifying query

I want to change:
/?q=bla
to
/search?q=bla
I have placed rule like:
RewriteRule ^search?q=(.*)$ /?q=$1 [L]
but it doesn't work, I would really appreciate some help, thanks
You can't match the query string using the pattern inside a RewriteRule. You need to match against the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^q=
RewriteRule ^search$ / [L,QSA]
Technically, you don't need the QSA flag, since query strings get appended automatically.

URL Rewrite using htaccess for one variable

My url is
domain.com/?p=slide&op=1
here i rewrite domain.com/slide using
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
I don't want to rewrite op=1, it should work when i enter
domain.com/slide?op=1
Any help would be appreciated!.
Sounds like you're looking for the QSA flag.
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
Will make it so that any query string is kept and appended to the new URL.

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

.htaccess redirect to a directory and retain $_GET variables?

I want to redirect from /orders/whatever/?n=4
to:
/orders/?shopURL=whatever&n=4
I managed to redirect to the correct directory but I'm losing the passed variables.
This is my current rule:
RewriteRule ^([\w\d]+)$ /orders/?shopURL=$1 [L]
If you want to enforce the existence of a query string, use:
RewriteCond %{QUERY_STRING} (\bn=\d+\b)
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1&%1 [L]
Otherwise, you can use the following without a RewriteCond:
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1 [L,QSA]
The QSA flag to RewriteRule will retain existing query string fields.
Your RewriteRule seems very generic, as though it would redirect almost every page.
I'm not that great with Regular Expressions so I'll leave those to you, or someone else. But you need something at the end of the rewrite rule to catch parameters, then append it at the end of the new URL.
RewriteRule ^([\w\d]+)/(somethinghere)$ /orders/?shopURL=$1&$2 [L]

Resources