I currently use the following redirect to make the urls look nicer:
RewriteRule profile/(.*) index.php?id=$1
(Result: domain.co.uk/profile/00000001)
From index.php?id=00000001
I would like to 301 redirect urls without www to www. and keep the above profile redirect.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
However, When this is used the URL comes out like:
http://www.DOMAIN.co.uk/ index.php /00000001 ?id=00000001
I’m assuming this can be done, Does this need to be bespoke for my needs or is there something im missing?
End result would ideally rewrite:
domain.co.uk/profile/00000001
to
www.domain.co.uk/profile/00000001
Keep rules in correct order i.e. keep external redirects before your internal rewrites.
So this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^profile/(.+)$ index.php?id=$1 [L,QSA,NC]
Related
I need to make a 301 redirect from
https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6
to
https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]
But no luck.
With your RewriteRule attempt, you demanded that the requested URL
ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)
With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]
OR only match the specific keyword
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]
I have an .htaccess file on an old domain of mine that includes redirects that aren't redirecting as I am wanting.
If I type in olddomain.com/about-us.html the redirect is sending the user to newdomain.com/about-us.html. What I am trying to do is direct the user to newdomain.com/about.
Below is my .htaccess file with one example of the redirect.
Does anyone see what I am doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule /about-us.html https://newdomain.com/about [L,R=301]
Redirect 301 /about-us.html https://newdomain.com/about
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Network Tab:
Have it like this:
RewriteEngine On
RewriteRule ^about-us\.html$ https://newdomain.com/about [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Make sure to test it in a new browser or completely clear browser cache.
Make sure there is no other code in your .htaccess when you test
I currently have two rewrite rules in my .htaccess file, and now need to add another. However, GTMetrix is already giving me an F rating here stating: Avoid landing page redirects
The first redirect adds the www. to the URL.
The second redirect adds a subdirectory to the URL.
The third (proposed) redirect adds the https to the beginning of the URL.
RewriteEngine on
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Then, rewrite to /catalog
RewriteCond %{REQUEST_URI} !^/catalog [NC]
RewriteRule ^(.*)$ /catalog/$1 [L]
#Now, rewrite to HTTPS:
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Is there any way to combine these into a single redirect? Or, is the landing page redirect not all that bad?
Your 1st and 3rd redirect rules can be combined into one and should be kept before rewrite rule:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#Then, rewrite to /catalog
RewriteCond %{REQUEST_URI} !^/catalog/ [NC]
RewriteRule ^(.*)$ /catalog/$1 [L]
Make sure to clear your browser cache before testing this change.
Allright so I have a website where I want traffic to go from
http -> https
non www -> www
So the final url should look like: https://www.x.net
I set up some rewrite rules in my htaccess, which look like the following:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
It works fine, everything gets redirected but the only problem is the following type of urls.
x.com/help
What happens is it will redirect to
https://www.x.comhelp
It removes the slash between com and help, I couldn't find an answer on Google so that's why I though, maybe my StackOverflow friend could help me out :)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
You should not use both the %{REQUEST_URI} server variable and the $1 backreference together. Use one or the other, see below...
You would need to change your directives to the following. This uses a backreference to the RewriteRule pattern which notably excludes the slash, so this must be included in the substitution:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=302,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=302,L]
Alternatively, use the %{REQUEST_URI} server variable instead. (This includes the slash prefix.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are sure it's working OK. (301 redirects are cached by the browser, so make sure your caches are cleared before testing.)
In fact im working on a small php script and im using somme htacces functions like rewrite but i have an problem , in order im redirection all urls dream-9.html to dream.php?id=9 using this code :
RewriteEngine On
RewriteRule dream-([0-9]+)\.html dream.php?id=$1 [L]
and recently ive decided to redirect all non www to www but i have a problem while redirecting from non-www to www the dream-9.html become dream.php?id=9 and its shown on the link this is the second code :
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/?(.*) http://%1/$1 [L,R=301,NE]
You need to keep rules in correct order, i.e. keep 301 rules before internal rewrite rules.
Have rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule dream-([0-9]+)\.html$ /dream.php?id=$1 [L,QSA,NC]