For one of my website I have to change the redirect match to rewrite rule to keep the modules in sync and alternatively remove the mod_alias .
I tried few but could not succeed can someone please help here.
RewriteCond %{HTTP_HOST} ^(.*)mywebsite.com [NC]
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mywebsite.com/$1/$2/$4
RedirectMatch 301 ^/search/page/(.*)$ http://www.mywebsite.com/?s&paged=$1
Tried to use the mod_rewrite but no success
RewriteCond %{HTTP_HOST} ^(.*)mywebsite.com [NC]
RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mywebsite.com/$1/$2/$4 [NC,L,R=301]
RewriteRule ^/search/page/(.*)$ http://www.mywebsite.com/?s&paged=$1 [NC,L,R=301]
You can use these 2 rules:
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(\d{4})/(\d{2})/(\d{2})/(.*)$ /$1/$2/$4 [NC,L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^search/page/(.+)$ /?s&paged=$1 [NC,L,R=301]
Related
For my website I want to force an URL with https, without www and without .php extension.
For that my htaccess file contains the following rules:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*) https://jvincent.fr/$1 [QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
It works fine.
But if I access to my website using the URL http://www.jvincent.fr/phpinfo.php I have two 301 redirects :
http://www.jvincent.fr/phpinfo.php to
https://jvincent.fr/phpinfo.php
https://jvincent.fr/phpinfo.php to https://jvincent.fr/phpinfo
I searched but I was not able to find a solution to optimize and have only one 301 redirect in all cases.
I there a way to do that? Merging all the conditions before an unique RewriteRule? Using environment variable (flag E in the first part)?
Thanks for your help.
Jack
You can use:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*?)(?:\.php)?$ https://jvincent.fr/$1 [NC,QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
I'm trying to redirect user request to my domain in .htaccess file.
My requirement is for example if user request for example.co/abc it should redirect to www.example.co/abc.
Below is my rewrite rule.
RewriteCond %{HTTP_HOST} ^example.co/Abc [NC]
RewriteRule ^(/Abc)$ https://www.example.co/$1 [L,R=301]
After adding the above two lines in my .htaccess file it is redirecting from example.co to www.example.co
I tried in different ways still not getting where i'm doing mistake.
Any help would be appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ http://www.example.co/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{www.example.co} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.co [NC]
#RewriteRule ^(/*)$ https://www.example.co/$1 [L,R=301]
RewriteRule ^/?(Abc)/?$ https://www.example.co/$1 [L,R=301]
I'm a newbie and the question is simple, I want mysite.eu and www.mysite.eu redirect to a subpage of their website,
but I can't get it to work for both because when I add something like this I got stuck in a loop so how can I fix this that both wil direct to that subpage without getting a loop.
My example:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
Why do you have the . there? sure you did not just want this:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
It can be done in a single rewrite rule like this:
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.eu$ [NC]
RewriteRule . page?page=webshop_fixol&lng=1 [L,QSA]
^(www\.)?mysite\.eu$ will match both sites www and non www.
I have this scenario where I would like to redirect my domains using the following scenario. Can you please advice if this can be achieved using RewriteRule in Apache?
I would like to redirect any calls made using http://www.domainname.com/url to redirect to http://domainname.com/url.
I was able to achieve the above using the following rewrite rule
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
# END WithoutWWW
If anyone tries to visit just http://www.domainname.com or http://domainname.com, I would like them to redirect to http://dname.com
How can I achieve this rule using RewriteRule in Apache?
I'm also using PHP, so a solution using PHP would be valid too.
Here is your combined .htaccess with your existing and new code:
RewriteEngine on
# redirect domainname.com/ or www.domainname.com/ to dname.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com [R=301,L]
# append www to domainname.com
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^ http://domainname.com%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
That example looks like mod_rewrite.
With mod_rewrite you could do:
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^(.*)$ http://dname.com/$1 [R=301,L]
# END WithoutWWW
You may want to look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Based on your comment you want to redirect the root differently so we could do:
RewriteEngine on
#www.domainname.com & domainname.com -> dname.com
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com/ [R=301,L]
#www.domainname.com/[url] -> domainname.com/[url]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
How to make .htaccess to remove question mark from URL if not ?id=(.*)?
# Rewrite for ?id=(.*)
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
# It does not work out on this way
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !^id=.*
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
This would be the right rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond !{QUERY_STRING} id
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]
Update:
# Query rewrite exceptions
RewriteCond %{QUERY_STRING} !callback=.*
Does this work?
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^(.*)$ $1?%1 [R=301,L]
Tip: during your testings, use 302 redirections instead of 301, as 301 redirections are stored by browsers. You can finally switch to classic 301 when you are done testing.