Remove characters from query string - .htaccess

I need to redirect
http://example.com/page/?Id=1253-23098-
to
http://example.com/page/1253-23098
This redirection also includes removal of '-' from end of query string.
What i've done is
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page/$
RewriteCond %{QUERY_STRING} ^Id=([0-9\-\0-9]*)$
RewriteRule ^(.*)$ http://example.com/page/%1? [L,R=301]
which is redirecting me to
http://example.com/page/1253-23098-
I need to removed anything after "1253-23098" from my query string.
I've googling it from last 3-4 hours buts nothing seems to work.
Any help is appreciated.

You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Id=(\d+-\d+) [NC]
RewriteRule ^page/?$ page/%1? [NC,L,R=301]

Try :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page/?$
RewriteCond %{QUERY_STRING} ^Id=([0-9]*)-([0-9]*)-$
RewriteRule ^(.*)$ http://example.com/page/%1-%2? [L,R=301]

Related

Htaccess appending to query string with QSA without redirect

I am trying to add English version to my website in subdomain level as following:
en.mywebsite.com/business-name : This will show the English version of a sub-page.
Right now I have actually achieved it somehow by appending the subdomain to the query string with the following code:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteCond %{HTTP_HOST} !^en\.mywebsite\.com [NC]
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
The problem is, it is doing it by redirecting the user to www.mywebsite.com/business-name?language=en, but I want to avoid redirection, and pass the "newly generated" query string virtually instead.
Can it be achieved, or does QSA always redirect?
Thanks in advance.
Try changing your last rule from:
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
to
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ /$1?language=en [L,QSA]

Multiple parameters URL redirection to another preety URL

I want to redirect via htaccess:
test.mydomain.com/articles_main.php?post=POSTTILE&name=&email=
To:
mydomain.com/POSTTILE
Any help is greatly appreciated, I have struggled with this for a long time, please help.
EDIT:
I have tested many things but what remained commented is this:
RewriteEngine On
RewriteBase
RewriteCond %{THE_REQUEST} \s/articles_main.\.php\?post=([^&]*)&name=([^&]*)&email=([^&]*) [NC]
RewriteRule ^articles_main\.php\?post=([A-Za-z0-9-]+)&name=&email=$ http://mydomain.com/$1 [L,R=301]
RewriteRule ^articles_main\.php\?post=([A-Za-z0-9]+)&?$ ^/$1/?$ [R=301]
Your regex is not correct. Use this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.mydomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/articles_main\.php\?post=([^&]+)&name=([^&]*)&email=([^&\s]*) [NC]
RewriteRule ^ http://mydomain.com/$1? [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^([A-Za-z0-9]+)/?$ articles_main\.php?post=$1name=&email= [L,QSA]

Htaccess redirect with query

I'm having an issue with a query string, although I'm not sure what i'm tryign to do is possible.
I have the following url with a search query attached:
subdomain.mysite.com/search/?search=searchquery
but I need it to redirect to the following url including the query string:
subdomain.mysite.com/search/?rs=searchquery
I was wondering if this is possible with a mod_rewrite?
I've tried the following:
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([0-9]*)$
RewriteRule ^(.*)$ http://subdomain.mysite.com/search/?rs=% [L,R=301]
I don't know if my Syntax is incorrect or i'm just barking up the wrong tree. Any help would be awesome.
This is the Start of my HTAccess File - This is a Wordpress site with the rewrite rule provided by user: Amine Hajyoussef:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]
</IfModule>
Unfortunately this doesnt appear to work? Any ideas?
you forgot the 1 after %, i have also changed the search string so it can be anything instead of just number.
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]

Htaccess Rewrite single query string

I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]
If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]

redirecting from short url

Let's say short.com is the short domain and long.com is the long domain
updated:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.long.com
RewriteRule (.*) http://long.com/ [R=301]
RewriteCond %{HTTP_HOST} ^short\.li$ [NC]
RewriteCond %{REQUEST_URI} !^/redirect
RewriteRule ^(.*)$ /redirect?short=$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)short\.li$
RewriteRule ^$ http://long.com/ [L,R=301]
both domains point to that root directory. When I type short.li I end up on long.com/?l=
how did I manage to screw up like that?^^
Try this in your htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)short\.com$
RewriteRule ^$ http://www.long.com/ [L,QSA,R=301]
Remove RewriteEngine on if it is already there
I think you might want something along the lines of this:
RewriteEngine on
RewriteCond {REQUEST_URI} !/
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/redirect.php?short=$1 [L,R=301]
RewriteCond {REQUEST_URI} /
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/ [L,R=301]
Not sure if the regex is needed on that last one or not, but something like this should work

Resources