Htaccess redirect with query - .htaccess

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]

Related

URL Rewriting syntax IIS 8 ISAPI Rewrite 3

I had a previous post regarding this matter:
ISAPI_Rewrite and Coldfusion Rules
With the help of Helicon Support we were able to create the following rules:
RewriteEngine on
RewriteRule on
RewriteBase /
RewriteCond %{QUERY_STRING} ^filter=brand&brand_id=([^&]+)&brand_name=([^&]+)$ [NC]
RewriteRule ^products-search\.cfm$ /search/%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([^/]+)/([^._]+)\.cfm$ /products-search.cfm?filter=brand&brand_id=$1&brand_name=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^product_id=([^&]+)&product_title=([^&]+)$
RewriteRule ^product-details\.cfm$ /%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^._]+)\.cfm$ /product-details.cfm?product_id=$1&product_title=$2 [NC,L]
Their help was much appreciated as i am very new to URL Rewriting. The issue i am having now is that the first rule works, it turns the url from:
www.mysite.com/products-search.cfm?filter=brand&brand_id=98&brand_name=HAMANN
to
www.mysite.com/search/98/HAMANN.cfm
Since i am new to this, i do not understand how to get the css/js/images folders to display on this page, what rule or condition do i use for the RewriteBase / part so that everything on the page loads correctly?
The second part of the .htaccess rules do not actually work which change:
product-details.cfm?product_id=1&product_title=This Is A New Product
to
products/This_Is_A_New_Product.cfm
Any help would be greatly appreciated

Dynamic URL redirect htaccess

First of all, I checked older questions on the topic but I can't get it to work still.
I basically want:
http://example.com/example/?test=3 to redirect to http://www.yahoo.com
I have:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /example/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /example/index.php [L]
RewriteCond %{QUERY_STRING} ^test=3$ [NC]
RewriteRule ^/example/index\.php$ http://www.yahoo.com [L,R=301]
</IfModule>
Ah yeah, I forgot that I already declared rewrite base. So, it should have been:
RewriteRule ^/index\.php$ instead of RewriteRule ^/example/index\.php$
Solved.
Put these lines just below your RewriteBase line:
RewriteCond %{QUERY_STRING} ^test=3$ [NC]
RewriteRule ^example/(index\.php|)$ http://www.yahoo.com? [L,R=301,NC]
And comment out your 2 lines below Wordpress rules.

.htaccess retrieve from folder if subdomain, not 301 redirecting?

I want my site to just point to /user folder if the request is a subdomain.
If the request is subdomain.site.com/admin, then the site should show the page for subdomain.site.com/user/admin.
The problem with my code is that it makes an 301 redirect instead of just keeping the url-address.
My code look like this:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)
RewriteRule ^$ user/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) user/$1 [L]
</IfModule>
And you can try for yourself, go to http://mickes.bildsida.se/admin and see how the address changes to /user/admin. Very disturbing...
You just need a few of the lines you showed to get this working.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
RewriteRule ^(.*)$ user/$1 [L]
I adjusted the HTTP_HOST checks because that parameter only looks at the domain name, so you don't need the (.*) at the end. I also removed the checks that look if the file exists or is a directory, since you want everything redirected (no reason to make it possible to access files from other subdomains, for example)
You code can be simplified to these lines : (try them instead)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
RewriteRule ^(.*)$ /user/$1 [QSA,L]
Finally! After days and nights of reading forums and documentations, and testing all possible ways, I got it to work!
Sulotion:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} !^bildsida.se
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ user/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? user/$1/ [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.+)/ user/$1 [QSA]
</IfModule>

How can I do this mod_rewrite?

I want to redirect all http://localhost/webportal/organizations/32 requests to http://localhost/webportal/organizations/32?qt-organization_tabs=tab1#qt-organization_tabs (where 32 is a variable).
How can I do this using mod_rewite?
Update:
I have updated the URLs above, the URLs originally posted were not correct, anyway I tried the following rule and it is not working:
RewriteRule ^/organizations/(.+)$ ^/organizations/$1?qt-organization_tabs=tab1#qt-organization_tabs [L,R]
All the other rules are working properly, here is the relevant part of my htaccess file (Drupal CMS):
RewriteEngine on
RewriteRule "(^|/)\." - [F]
RewriteBase /webportal
RewriteRule ^/organizations/(.+)$ ^/organizations/$1?qt-organization_tabs=tab1#qt-organization_tabs [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
Thanks
Add this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^/?webportal/organizations/(.+)$ /pepris/webportal/$1?qt-organization_tabs=tab1#qt-organization_tabs [L,R]
You can also use mod_alias:
RedirectMatch ^/?webportal/organizations/(.+)$ /pepris/webportal/$1?qt-organization_tabs=tab1#qt-organization_tabs

.htaccess doesn't work

I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mykeyword$ news.php [L,QSA,NC]
However, when I open the news.php, the url is still the same, that is www.mydomain.com/news.php instead of www.mydomain.com/mykeyword
I make the following test:
RewriteEngine on
RewriteRule ^test\.html$ test.php [L]
I upload 2 files on my server, test.html and test.php and after I type www.mydomain.com/test.html, my php page was displayed, so that mean that I have no problem with my settings. What on earth I am doing wrong???
Any help will be deeply appreciated.
Regards,Zoran
Change your .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The rewrite rule translates from the URL supplied by the user to the URL seen by the server. Try browsing to www.mydomain.com/mykeyword - you should see the page news.php.

Resources