Problem with .htaccess redirect error - .htaccess

I'm creating a custom .htaccess redirect file and I'm running in to a strange problem
Here is my .htaccess file rules
RewriteCond %{HTTP_HOST} !^www\.site\.com$
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.site.com/$1/ [R=301,L]
RewriteRule ^postreview/ /viewreview.php [NC,PT]
RewriteRule ^([^/\.]*)/?([^/\.]*)/?([^/\.]*)/?$ /template2.php?slash1=$1&slash2=$2&slash3=$3 [L]
To sum up what I am trying to achieve:
1. Force www
2. Have the 3 slash areas translated to variables for tempalte2.php
3. send postreview to viewreview.php
Most of what I am trying to do is working, but I have a strange bug.
When I enter "site.com/slash1" It redirect to "www.site.com/site.com/slash1/"
But if I enter "site.com/slash1" it redirects to www.site.com/slash1/
For some reason it also adds a "/" to the end of everything. Not sure why..
Any ideas?

For some reason it also adds a "/" to the end of everything. Not sure why..
It's due to this rule
RewriteRule (.*)$ http://www.site.com/$1/ [R=301,L]
Change it to
RewriteRule (.*)$ http://www.site.com/$1 [R=301,L]

Related

How To Remove .cgi From URL With .htaccess?

I use a .cgi file to run python on my server. However, whenever someone goes to the site, it brings them to https://example.com/main.cgi/ (main.cgi is the name of the cgi file). Is there a way to remove this from the .htacess file, so it goes to https://example.com/ but still runs the cgi file?
This is the current .cgi file:
RewriteEngine On
RewriteBase /website
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
How can I make a RewriteRule that makes the URL / go to /main.cgi/ without it appearing in the URL?
You should make your https implementation as a first rule of your .htaccess file, could you please try following. Written as per your shown samples, please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##Rule for applying https to non-http calls.
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Look for if any page doesn't have `/`slashes at the end then add it.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R=301,L]
##Rules for non exiting file or directory to main.cgi file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ main.cgi [L]
NOTE: Using RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] personally gives me 1 slash at end so I had written a separate rule for adding trailing / for this one, to avoid 2 times / at the end of url.

.htaccess redirect to subfolder, non-www to www and https

So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]

htaccess redirect not working properly

I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]

.htaccess not working properly

I changed .htacess file as below.
RewriteCond %{REQUEST_URI} (.*)product-list.php(.*)
RewriteRule (.*) www.example.com/swimming-pool/product-list\.php$1 [R=301,L]
RewriteCond %{REQUEST_URI} (.*)product-info.php(.*)
RewriteRule (.*)\?(.*)$ www.example.com/swimming-pool/product\-info\.php$2 [R=301,L]
i just need that when i request for
http://www.example.com/product-info.php?Applepc.html should be redirected to
http://www.example.com/swimming-pool/product-info.php?Applepc.html
Output is getting like this in URL field:-
www.example.com/swimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpproduct-list.php?Flowers-pg1-cid38.html
Please tell me where i am mistaking.
Although you don't mention what is the purpose of the other rule with product-list.php, it is included in this rule-set.
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (product-list|product-info)\.php [NC]
RewriteRule ^(.*)/?$ swimming-pool/$1 [R=301,L]
Redirects permanently any URL like this one
http://www.example.com/product-info.php?query or
http://www.example.com/product-list.php?query
To
http://www.example.com/swimming-pool/product-info.php?query or
http://www.example.com/swimming-pool/product-list.php?query
It seems the problem with the actual rules is they are generating a loop.

How to replace "-" with "_" and remove index.php from the address bar

I have developed my website in codeigniter. I have a couple of things to accomplish in .htaccess file.
For any page/resource other than index.php, img, js, css i want it to add index.php before the actual resource in the url.
I have so many static pages having unserscore "_" in them, e.g, contact_us.php, our_services.php etc. I want when user gives the url like www.mywebsite.com/our-services it should open up the original page, that is, www.mywebsite.com/our_services. There are 1,2 upto 7 underscores for different pages, e.g, mywebsite.com/speech_writing_services
Here is my .htaccess file which i could build up so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
The problem here is that when i give the url like www.mywebsite.com/contact_us it works just fine and shows the same url in the address bar. But when i give this url www.mywebsite.com/contact-us it does show the page but the url shown in the address bar of the browser becomes www.mywebsite.com/index.php/contact_us whereas i want it to be like www.mywebsite.com/contact_us with index.php removed.
You need to do the routing after the redirecting.
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources