Replacing - sign with + Query string in htaccess - .htaccess

I have tried my best but ony little of it works, I want to replace - to + sing withing a specific query string in a category.
i want to replace
https://www.example.com/stack/?s=over-flow
to
https://www.example.com/stack/?s=over+flow
I have tried my best with the below code
RewriteCond %{QUERY_STRING} ^(.+)-(.+)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
It works but it breaks other part of my site containing - sign in the url
I want it specifically on /stack/?s= only
Please help me out, thanks in advance

You may use this rule to target a specific query string with only single parameter:
RewriteCond %{QUERY_STRING} ^(s=[^&-]+)-([^&]+)$ [NC]
RewriteRule ^(stack/?)$ /$1?%1+%2 [L,R=301,NE]

Related

How to rewrite query string to hash in the URL?

I am looking to rewrite:
www.mysite.com/services/category1/?content=service1
to
www.mysite.com/services/category1/#tab-service1
I have this specific rewrite that I want to generalize for all the possible URLs on the site:
RewriteCond %{QUERY_STRING} content=service1
RewriteRule ^services/category1 services/category1/#tab-service1? [NE,R,L]
I tried the following but it didn't work :( -
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?content=(.*)$ services/category1/#tab-$1 [NE,R,L]
Thanks.
You rule looks fine except for 1 thing
QUERYSTRING content=service1
isn't part of match in RewriteRule's pattern so you can't test query string (Url part after the ?) in pattern of a RewriteRule. To test url query string we use %{QUERY_STRING} variable like the one you are already using.
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?$ services/category1/#tab-%1 [NE,R,L]

removing a query string

I have 60 or so old product URLS like this:
http://www.domain.com/page.php?pid=Product Name
I can rewrite the like this.
RewriteCond %{QUERY_STRING} pid=Product%20Name [NC]
RewriteRule ^page\.php /my/new/url/for/Product_Name [R=301,NE,L]
But this leaves the query string on the new url like this
http://www.domain.com/my/new/url/for/Product_Name?pid=Product%20Name
1/ Is there a way to remove the query string?
2/ Do I need to do the same for all 60 products? Fine but just a lot of copy/paste.
Thanks as ever.
If you don't care about other params:
RewriteCond %{QUERY_STRING} pid=([^&]*) [NC]
RewriteRule ^page\.php /my/new/url/for/%1? [R=301,NE,L]
If you want to save other params, use:
RewriteCond %{QUERY_STRING} ^(.*)pid=([^&]*)&?(.*)$ [NC]
RewriteRule ^page\.php /my/new/url/for/%2?%1%3 [R=301,NE,L,QSA]
This will leave a hanging ? when there are no other parameters, or a hanging & when pid is the last param on the list, but these are still valid urls.
Putting a question mark at the end of the target will clear the query string.
RewriteRule ^page\.php /my/new/url/for/Product_Name? [R=301,NE,L]
If you find yourself repeating the same pattern many times then consider using RewriteMap to specify a program to process the URL.

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.

Pattern match 301, retaining one querystring parameter

Been scratching my head over this one. I have a URL format I need to redirect and only need one part of the querystring. Example URL:
tiles?shape=&colour=&finish=&material=&price=&o=60&tile=50-double-roman-antique-red-03-granular
All I need from this is the value of the tile parameter (50-double-roman-antique-red-03-granular) so I can then redirect it to:
tiles/detail/50-double-roman-antique-red-03-granular
Any suggestions for a rewrite rule?
Thanks
This works on my box. We're including the ? after the rewrite string to prevent the behavior or QSA or query string append which is the default behavior if no query string is passed. If you forget the ? then it creates a loop.
RewriteCond %{QUERY_STRING} tile=([^&]+)&?.*$
RewriteRule (.*) /tiles/detail/%1? [L]
This off-topic answer gave me what I was looking for:
RewriteCond %{QUERY_STRING} &tile=([0-9a-zA-Z-]+)
RewriteCond %{REQUEST_URI} !tiles/detail
RewriteRule tiles /tiles/detail/%1? [R=301]

.htaccess with variables for user friendly urls

I got these two urls:
/portfolio/stamped_concrete
/p_details.php?id_cat=23&?id=91
I want to make the second url rewrite to:
/portfolio/stamped_concrete/23/91
stamped_concrete is a dynamic url which is why I'm at a loss of how to solve this. Also the two files (portfolio.php and p_details.php) are in the same directory if that matters.
How would I accomplish this?
EDIT:
stamped_concrete is also a variable string that I rewrote before and it works:
RewriteRule ^services/([a-z0-9_-]+)$ /services.php?url=$1 [NC,L,QSA]
so how would I call it within the RewriteRule?
would this be on the right track?
RewriteCond %{QUERY_STRING} url=([a-z0-9_-]+)
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/$1/$2/$3
Try this:
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/stamped_concrete/$1/$2
Might need to tweak it a bit -- not sure if the RewriteRule part is correct (sorry).
But, the important part is QUERY_STRING, see the apache docs: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also, from http://wiki.apache.org/httpd/RewriteQueryString
I always mix up the backreferences, so try it out, it's eiher dollar signs or percent signs (i really thought it was dollar signs...)
(QUOTE)
Making the Query String Part of the Path
Take a URL of the form http://example.com/path?var=val and transform it into http://example.com/path/var/val. Note that this particular example will work only for a single var=val pair containing only letters, numbers, and the underscore character.
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
(END QUOTE)
So you could probably just say "RewriteRule ^/p_details.php /portfolio/%1/%2/%3"

Resources