Rewriting a weird URL string with .htaccess - wordpress

I'd like rewrite a URL string from ?s= to /search/ with .htaccess.
Example: http://hello.com/?s=hey => http://hello.com/search/hey
How can I do this?

May be something like this
RewriteEngine On
RewriteRule ^search/(.*) index.php?s=$1 [QSA,L]
This way you will be accessing your site like
http://hello.com/search/hey will be interpreted as http://hello.com/index.php?s=hey
hope this helps

Put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from index.php?s=key to search/key
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?s=([^\s]+) [NC]
RewriteRule ^ search/%1? [L,R,NC]
# external redirect from search/key to index.php?s=key
RewriteRule ^search/(.+)$ index.php?s=$1 [L,NC,QSA]

Thanks for the answers guys, this works for me, taken from here.
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [R,L]

Related

Redirection with HTACCESS including GET parameters

I have a specific .htaccess defined as is, in order to reroute all traffic from root folder to identical URL in a subfolder with exceptions on sub-domains.
Here's my file:
#RewriteEngine on
#RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !^joe(.*)
RewriteCond %{REQUEST_URI} !^lab(.*)
RedirectMatch ^/$ http://www.mydomain.fr/blog/
</IfModule>
This file does exactly what I want: when I input something like www.mydomain.fr/wp-admin for example, it will redirect to www.mydomain.fr/blog/wp-admin, but will exclude subdomains joe.mydomain.fr and lab.mydomain.fr from redirection.
However, what happens is that if I go to joe.mydomain.fr?test=1, the server will simply redirect me to joe.mydomain.fr but the GET parameter disappears.
How can I avoid that?
I tried replacing [NC] with [QSA] or [R=301,QSA], but I get an Internal Server Error.
Thanks.
After spending my day documenting about how .htaccess files actually work, I was finally able to solve my problem with the following file, inspired by the one from Abhishek Gurjar:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /s
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^blog\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !/joe
RewriteCond %{REQUEST_URI} !/lab
RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301]
</IfModule>
FYI, the 3 first lines are mandatory with my provider 1&1 apparently...
What I changed is the REQUEST_URI from being !^joe(.* ) and !^lab(.*) to !/joe and !/lab and then excluding the blog.mydomain.fr URL as well to avoid problems with blog.mydomain.fr/joe or blog.mydomain.fr/lab if ever one day I use them.
Finally, I had to redirect my sub-domains in my 1&1 client interface to the subfolders of my webspace instead of the URL in hard, meaning:
Instead of redirecting joe.mydomain.fr to http://www.mydomain.fr/joe, I redirect it to /www/joe/
That configuration now allows me to type in an URL such as joe.mydomain.fr?dnt=1 while getting redirected to http://www.mydomain.fr/joe?dnt=1 which is exactly what I wanted to do.
I hope this helps others.
Thanks for the help.

(.*) redirect loop Wordpress

Trying to proper make a redirect loop for site
tryng to redirect all sites like
34www.arsenal.org.pl / 46www.arsenal.org.pl / ww32w.arsenal.org.pl / dalx.arsenal.org.pl
to arsenal.org.pl
http://www.webconfs.com/htaccess-redirect-generator.php gave me the code
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://arsenal.org.pl/$1 [R=301,L]
but it give redirect loop in Wordpress
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)arsenal.org.pl$
RewriteCond %{HTTP_HOST} ^(.*?)\.arsenal.org.pl$
RewriteRule ^(.*)$ http://arsenal.org.pl/$1 [r=301,nc]
ty Cbroe for hint :)

Can't keep query string on URL

I run an Apache server with ISPConfig 3 installed and WordPress. I applied rewrite rules so that all HTTP goes to HTTPS and all WWW goes to non-WWW.
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com$1 [QSA,L]
I put this in my WordPress htaccess, and the example.com.vhost file created by ISPConfig 3.
The problem is now, even with the QSA in the flags, the query string is removed. I have tried the flags:
[L,R]
[R=301,L]
[R=301,L,QSA]
[L,QSA]
[QSA,L]
I have checked apache2.conf, 000-default from ISPConfig, example.com.vhost, and .htaccess files for any rules that do not have QSA or R and have not found any.
I'm a RewriteRule novice, but I'm trying to wrap my head around this, what am I doing wrong?
EDIT: While in my WP dashboard, I noticed that query strings are present in the URLs. So while on the frontend my server drops the query string, on the backend the query string is preserved. So I'm thinking that the WordPress's .htaccess must be the issue, right?
Try with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Test for www before test with https
No need to add [QSA] if you do not add ? and new query string.
While experimenting never use the R=301 permanent redirect because it will make live very hard for you. With Firefox you need to delete the entire cache just to try again. A useful trick can be using the private navigation mode to test. Then at least you can ditch it and start up another one.
Query strings would be transferred automatically in a [R] redirect. Unless maybe there is some QSD before or you put a ? inside the RewriteRule.
Can you try it like this?
RewriteEngine on
RewriteCond %{HTTPS} ^off$
RewriteRule . https://example.com%{REQUEST_URI} [R,L]
RewriteEngine on
RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule . https://example.com%{REQUEST_URI} [R,L]

Redirect to https for certain url only for wordpress site

I'm working on a wordpress site. And just installed SSL. It has been installed correctly but I want to redirect visitors to the https url for certain pages only. I also want to force browser to use http for other pages.
I know this can be done with .htaccess and tried several things as well. But unable to get this as I need. I'm a novice at handling .haccess rewrite rules and can't find the docs that can guide me.
For example, I need to force browser to use https for this two urls:
http://www.example.com/sells/payment/
http://www.example.com/customer/login/
and for all other urls to just use normal http forcefully. What kind of rules I need to write?
Update 1
I also have a rule that redirects non-www url to a www url, and that might be conflicting with these rules. Here is how I redirect all non-www urls to www urls.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The issue I'm facing is, after applying https rules, it is redirected to https://www.www.example.com/sells/payment/ which is a wrong url.
Any idea for fixing this?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(sells/payment|customer/login)/ - [E=MY_URL:1]
RewriteCond %{HTTPS} off
RewriteCond %{ENV:MY_URL} 1
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} on
RewriteCond %{ENV:MY_URL} !=1
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
UPDATE:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(sells/payment|customer/login)/ - [E=MY_URL:1]
RewriteCond %{HTTPS} off
RewriteCond %{ENV:MY_URL} 1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} on
RewriteCond %{ENV:MY_URL} !=1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
This what I've used consistently across my projects where I have similar use-cases as yourself:
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [env=askapache:%2]
# redirect urls with index.html to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http%{ENV:askapache}://%{HTTP_HOST}/$1 [R=301,L]
# change // to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*)\ HTTP/ [NC]
RewriteRule ^.*$ http%{ENV:askapache}://%{HTTP_HOST}/%1/%2 [R=301,L]
This is an excerpt from a site where I found the solution, so I can't take credit for it:
Smart HTTP and HTTPS .htaccess Rewrite
I haven't tried it, but can you handle it the same way you do cononcial URLs?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com/your-page
RewriteRule (.*) https://www.example.com/your-page [R=301,L]

RewriteEngine Folder Redirect

I am writing a rewrite rule to redirect all urls from mysite.co.uk/en/ to mysite.co.uk/. I have the following I have written. Just would like someone to confirm it is correct for me and suggest possible improvements if any.
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co\.uk$
RewriteRule ^en/(.*)$ http://www\.mysite\.co\.uk/$1 [NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com\.au$
RewriteRule ^en/(.*)$ http://www\.mysite\.com\.au/$1 [NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co\.nz$
RewriteRule ^en/(.*)$ http://www\.mysite\.com\.nz/$1 [NC,R=301]
RewriteEngine off
After much research turns out I should be able to do just this:
redirect 301 /en/ http://www.mywebsite.co.uk/
redirect 301 /en/ http://www.mywebsite.com.au/
redirect 301 /en/ http://www.mywebsite.co.nz/
This we redirect any urls that are going to www.mywebsite.co.uk/en/ or mywebsite.co.uk/en/ to www.mywebsite.co.uk/
Use below code
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.

Resources