I want to combine these two rules, but not sure how
RewriteRule ^([^\.]+)$ $1.html [L]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
when I put both, I get the error "too many redirects"
My goal here is to combine them both,
the first rule is to remove file extensions (ex. html)
the second rule is: make every URL go to https://www.example.com, rather than https://example.com
With the additional information you now gave I would suggest this approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,END]
RewriteRule ^/?(.+)\.html$ /$1 [R=301,END]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
In general it is a good diea to start using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues ...
Related
I have a .htaccess file and a couple of redirect rule.
For example:
RewriteRule ^blog/migrations/?$ / [R=301,L,NE]
RewriteRule ^blog/digital/?$ / [R=301,L,NE]
Basically, all my blog urls have been removed but i found redirects rule for every single blog url to redirect to main page. But i want this to be in one rule, something like this:
RewriteRule ^blog/{anyUrl}/?$ / [R=301,L,NE]
Also i have strange redirects, such as:
RewriteRule ^2/?$ / [R=301,L,NE]
RewriteRule ^3/?$ / [R=301,L,NE]
RewriteRule ^4/?$ / [R=301,L,NE]
RewriteRule ^5/?$ / [R=301,L,NE]
RewriteRule ^6/?$ / [R=301,L,NE]
And i want them to be one rule, for example:
RewriteRule ^{[2-6]}/?$ / [R=301,L,NE]
Can anyone help with those rule, i am not really good at regexps.
//Edited
Also i have a rule that redirects urls with query strings:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)/?$ /$1 [QSD,R=301,L]
But this rule removes all query strings. How can i exclude from the rule specific query strings that starts with utm_?
And how can i get the domain name? I have my website on two different domains. For example:
RewriteRule ^posts/? https://{domain}/somewhere
You can combine all those rules into one rule using regex alternation:
RewriteRule ^(blog/|[2-6]/?$) https://%{HTTP_HOST}/ [R=301,L,NC]
This pattern ^(blog/|[2-6]/?$) will match start position and then either blog/ or [2-6] followed by optional / and end position.
For the other query string rule you can use:
RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(^|&)utm_ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSD,R=301,L,NE]
RewriteRule ^posts/? https://{domain}/somewhere
You don't need to use https://%{HTTP_HOST} here in redirect rules to make it same domain as in the request, so make it:
RewriteRule ^posts/? https://%{HTTP_HOST}/somewhere [L,R]
This has been driving me insane. Any help appreciated!
I have a site that has a bunch of dynamic entries in the format:
http://example.com/foo?item-1
http://example.com/foo?item-2
etc.
Noting there is no equal sign / parameter post the ?, just a single string per the above.
I want to clean up the URL's so that we get to:
http://example.com/foo/item-1
So basically, I just want to remove that pesky question mark for foo pages and replace it with a slash. I haven't seen anyone address this case previously. I have tried the following with no success:
RewriteRule ^foo/([^/]*)\$ /foo?$1 [L]
There are two other rules in use already (both of which work). These remove the php from the end of the URL and the http from the start, per the below.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
Thanks again in advance.
You have a problem with your regex. The \ before the $ symbol means you literally want to match a dollar sign there. Try without the \.
Also, you probably want that before your php rule but after the redirect. You want your external redirects before your internal rewrites:
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule ^foo/([^/]*)$ /foo?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule files/name/(.*).(pdf)$/ download.php?file=$1.$2&tkn=token
RewriteRule files/name/(.*).(pdf)$ download.php?file=$1.$2&tkn=token
RewriteRule files/name2/(.*).(pdf)$/ download2.php?file=$1.$2&tkn=token
RewriteRule files/name2/(.*).(pdf)$ download2.php?file=$1.$2&tkn=token
The folders name and name2 contain identical files. download.php and download2.php are identical.
The files/name/(.*) rewrite rule will redirect nearly all files to download.php, but fails on, what seems to be long file names. Two files I know it has failed on are:
abcdefghijklmn-abcdefghij.pdf
Pbcdefg ab abc Abdefghij 2.PDF
The files/name2/(.*) rule catches all files, including the two that are failing in the first rule. Literally, if I go to http://www.domain.com/files/names/abcdefghijklmn-abcdefghij.pdf, the server will serve me the file directly. If I go to http://www.domain.com/files/names2/abcdefghijklmn-abcdefghij.pdf, (just add 2 to names), the server will redirect me to http://www.domain.com/download2.php?file=abcdefghijklmn-abcdefghij.pdf&tkn=token
I've been trying to get this to work for hours, and I have no idea why the first rule won't work. Is there some weird idiosyncrasy I'm missing?
Try it this way and see how it works for you.
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule files/name2/(.*)\.pdf$ /download2.php?file=$1.pdf&tkn=token [R,L]
RewriteRule files/name/(.*)\.pdf$ /download.php?file=$1.pdf&tkn=token [R,L]
I have these vales in my htacces file it all works fine except when i try to go back to http from https i have tried swapping the rules around with no success, any help would be awsome
I have tried all the sujestion with still no suucess so maybe i need to show you guys the entire thing.
Still not going back to http from https, here is the whole thing
Have i got the rules in the wrong order? im lost
<ifModule mod_rewrite.c>
RewriteEngine on
# For Sales:
RewriteRule ^shop/sales/?$ sales.php
# For the primary categories:
RewriteRule ^shop/([A-Z-Aa-z\+]+)/?$ shop.php?type=$1
# For specific products:
RewriteRule ^browse/([A-Za-z\+]+)/([A-Za-z\+\-]+)/([0-9]+)$ browse.php?type=$1&category=$2&id=$3
#For https pages:
#RewriteCond %{HTTPS} on
#RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L=301]
#RewriteCond %{HTTPS} off
#RewriteRule ^(checkout\.php|final\.php|admin/(.*))$ https://{HTTP_HOST}/$1[R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
</ifModule>
I am not able to test the rules, but I think you need to change the following:
In the first rule your flag is attached to the 2nd argument. This should create an internal error. Your second rule would rewrite all url's to their http equivalent, making an infinite loop. You need to make sure it doesn't match url's that you want to be in https. You can do this with %{REQUEST_URI} and a negation (!). As far as I am aware, L=301 is an invalid flag too. You probably meant to make it R=301.
RewriteCond %{HTTPS} off
RewriteRule ^(checkout\.php|final\.php|admin/(.*))$ https://{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/(checkout\.php|final\.php|admin/(.*))$
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Last but not least an word of advice. Don't test your .htaccess with permanent redirects until everything works as expected. The browser will cache permanent redirects, not picking up further tries to make your .htaccess work as you want.
Earlier the admin RewriteRule was effecting scripts/admin.js until I added the RewriteCond.
Then I noticed that scripts/members.js was being effected by the same rule with the member context.
I added the exact same RewriteCond after as is below...
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/members(.+) members$1 [QSA]
Instead of having to manually add a RewriteCond every time to deal with issues how can I merge these rules please?
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]
You can merge your three checks into one like this:
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/(admin|blog|forms)(.+) $1$2 [QSA]
But it does seem like a rule to simply stop processing for js/css files will be a better option for you, unless you actually need rules applied to them for some reason.
You can invert the condition and make a pass-through rule that essentially tells mod_rewrite to stop. This way, you have 1 RewriteCond, 1 extra RewriteRule that is applied via the condition, and then everything else:
# Just one of these, note that there is no !
RewriteCond %{REQUEST_URI} \.(css|js|zip)$
# pass through, if the request is for a css/js/zip, stop rewriting
RewriteRule ^ - [L]
# otherwise, do some rewriting
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]
This will, however, affect any other rules you have that could apply to requests to css/js/zip, because this will cause all requests for those extensions to bypass all rewriting.