.htaccess redirection on local server - .htaccess

One of my doamins has been indexed by Google in various ways, mail.mydomain.com, ftp.mydomain.com, pop.mydomain.com... instead of simply www.mydomain.com.
I have inserted the following two lines in my .htaccess file and this seems to be working well in telling Google to update it's index:
RewriteCond %{HTTP_HOST} !^(www)\.mydomain\.com [NC]
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
However, when these same two lines are included on the .htaccess file of my local server (127.0.0.1) I can no longer reach 127.0.0.1 as I'm always redirected to www.mydomain.com.
does anybody know how to write these two lines such that I don't have to comment them out on my local server?

I've dealt with that problem by putting the relevant lines into the Apache configuration, and then having separate live and development configurations. They are likely required anyway for file-path reasons and servernames, if nothing else.

Related

Doing URL Rewriting and Redirections (.htaccess) with same code working both on WAMP localhost and remote folder?

first of all, I'm sorry for this umpteenth topic about this kind of problem.
I've tried to adapt other topics to my problem for months without being able to figure out how to do. That's why I'm writing today, it's really bugging me and as of today, I can only successfully do URL rewriting on remote but always coding in production remote folder isn't reliable solution.
This is what my file tree look like :
Firstly, I'd like to be redirected from the root of this 'template1' project to the root of the 'public' subfolder ; and secondly, I'd like to rewrite URLs so this kind of URL :
template1/index.php?route=qwertyuiop
turns into that :
template1/qwertyuiop
Both these things I want work but only on my remote folder on Ionos and I had to do weird things. For example for the redirection, I'm directly linking domain to subfolder and for URL rewriting I do :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.pdf|\.css|\.svg|\.js)$
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
Anyways, this works fine on remote folders and when I do PHP header("location:/azertyuiop") it works fine but in localhost it tries to search at the root of WAMPserver and I don't know how to proceed to fix it while keeping same code for both local and remote.
If any of you could help me figure out how to deal with this problem, it would be really appreciated as reading topics and putting whatever I can in my .htaccess doesn't work on localhost.
Ok, sooo thanks #MrWhite for basically telling me about VirtualHost. I never saw about it in any topics so I was searching maybe in the wrong direction.
Everything is working fine, now. So in case in the future someone gets through it, I will explain what I did to make it work in localhost. What I will tell is translated from a website I've found in my native language ( How to configure VirtualHost (French) ).
STEP 1 : edit Apache's config in Wamp
In the 'httpd-vhosts.conf' file located in 'C:/wamp64/bin/apache/apache2.4.46/conf/extra/httpd-vhosts.conf' (in my case), I had to add these virtual host lines :
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.localhost
DocumentRoot "${INSTALL_DIR}/www/projets/projets_persos/tests/template1/public"
ServerName template1.local
ServerAlias template1.local
ErrorLog "logs/template1.local-error.log"
CustomLog "logs/template1.local-access.log" common
</VirtualHost>
Then I had to modify Apache's main config file called 'httpd.conf' located in 'C:/wamp/bin/apache/apache2.4.46/conf/httpd.conf' (in my case) and uncomment the following line :
#Include conf/extra/httpd-vhosts.conf (in my case, it was already uncommented)
After that, the website says to restart Wamp's services before step 2, which I did after step 2 below and it stills work without any trouble. So anyway, I guess it will be same for you, do it now or after step 2.
STEP 2 : edit Windows' hosts file
At bottom of the 'hosts' file located in 'C:/Windows/System32/drivers/etc/hosts' I added the following line at bottom of the file to override the previous rule :
127.0.0.1 template1.local
Aaand, that's all.
=-=-=-=-=
As a proof it's working well, here's a screenshot of my project :
Here you can see on the picture I exploded my route in an array. I do this for personal purposes, to be able to route according to whatever bit of string I want. If you want to get rid of the 'public' value in the route, just type $_GET['route'] = str_replace('public/', '', $_GET['route']); before putting each of its fields in the array. Then, it will look like this :
Also, of course, I used .htaccess to be able to do URL Rewriting, here's my .htaccess file :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.pdf|\.css|\.svg|\.js)$
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
It's a bit off topic but in case you'd need to know, the RewriteCond lines allow to type in the true URL of assets that have these extensions and it won't rewrite as they are true URLs.
=-=-=-=-=
Anyway, this is the end of my solution, thanks again for helping me and all's well that ends well. The End.

Subdomain in .htaccess file only works with index

I have a subdomain setup in my .htaccess, which only seems to work with the default index.html page. I'd LIKE it to work for ANY page in the folder corresponding to the subdomain. Edited for privacy, assume my domain is example.org. The pertinent parts of the file look like this...
#subdomain
RewriteCond %{HTTP_HOST} ^subname\.example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subname\.example\.org$
# (a few lines added by my hosting company deleted -- see below)
RewriteRule ^/?$ "http\:\/\/example\.org\/subname\/" [R=301,L]
So the result of the above is that if I have an index.html page in my 'public-html' (root?), http://example.org and a different index.html stored in a sub-folder (having the same name as the subdomain), I will get this expected result, which works...
browse to: http://example.org results in viewing http:// example.org/index.html
browse to: http://subname.example.org results in viewing http:// example.org/subname/index.html
Great so far. This is what I expected when I created the domain name. However, given a specific file myfile.html stored in the subname folder, I would expect this to work also, and it doesn't...
browse to: http://subname.example.org/myfile.html results in a 404 error.
This despite the fact that browsing to http://example.org/subname/myfile.html works fine. In that case myfile.html is displayed. So is there anything I can do to modify the subdomain code to get the result I'm looking for? Namely, browsing to http://subname.example.org/ANYFILE should work as well as browsing to http://example.org/subname/ANYFILE, regardless of what 'ANYFILE' is. This, after all, is one of the main reasons I set up the subdomain to begin with!
Note: I confess that I relied on my hosting company's cPanel utility to create the subdomain code, so I asked for their tech support for help first. Long story short they didn't. Maybe what I hoped for is not actually possible?
Also, the lines I deleted' from the code had to do with something called "well-known/acme-challenge", added by my hosting company at some point. Since removing them had no effect on the behavior I've described, I left it out to avoid clouding the issue.
RewriteRule ^/?$ "http\:\/\/example\.org\/subname\/" [R=301,L]
This only "redirects" the document root. To redirect all URLs you need to change the above to read something like:
RewriteRule (.*) http://example.org/subname/$1 [R=301,L]
The $1 backreference refers to the URL-path captured in the RewriteRule pattern, ie. (.*).
No need to backslash-escape the colons, slashes and dots in the substitution string (that's typical of cPanel).
Also, the lines I deleted' from the code had to do with something called "well-known/acme-challenge", added by my hosting company at some point.
Those lines will likely be required when the (Let's Encrypt?) SSL cert auto-renews. (Although the above redirects to "http" - are you not using HTTPS?)
UPDATE:
RewriteCond %{HTTP_HOST} ^subname\.example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subname\.example\.org$
Just as an aside, these two conditions could be reduced to a single condition if you wanted. For example, the above is equivalent to:
RewriteCond %{HTTP_HOST} ^(www\.)?subname\.example\.org$

How do I remove a folder from a URL?

First of all, this question has been asked a few times on stack, however, none of the answers seem to work for me.
I have a website which has a "pages" folder in the root, I want to store all of my website pages in there.
Here's my structure
/pages/folder/folder2/index.php
I want to make it so the link displays:
https://wwww.website.com/folder/folder2/index.php
Removing the "/pages/" part of the URL, when I try all of the answers suggested previously, I get a 404 error.
Here is the htaccess I'm using
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^pages(/.*|)$ $1 [L,NC]
</IfModule>
and i also tried:
RewriteEngine On
RewriteRule ^pages/(.*)$ /$1 [L,R=301]
This htaccess is in the root. I can't seem to get it working, can anyone offer any suggestions? Thank you!
Your second attempt looks fine, though it can be imporoved a bit:
RewriteEngine On
RewriteRule ^/?pages/(.*)$ /$1 [R=301]
That rule should work inside the http servers host configuration or in some dynamic configuration file (".htaccess" style file) if the http server's rewriting module is loaded. You definitely should prefer the first option, but if you really need to use a dynamic configuration file then take care that the interpretation of such files is configured at all and that the file is placed in your hosts's DOCUMENT_ROOT folder and is readable for the http server process.
If that does not work then you need to start debugging. In this case you will start by monitoring your http server's error log file while making test requests. If no hint appears in there you need to enable rewrite logging which allows you to learn what exactly is going on inside the rewriting engine. See the official dpcumentation for the apache rewriting module for that. As typcial for OpenSource software it is of excellent quality and comes with great examples.

htaccess works with www.example.com but not example.com

I have installed Ghost, which needs nodejs to run. I'm doing this on an Apache Linux server via managed hosting. They kindly let me login with SSH access so I've been able to setup nodejs and Ghost using the standard installation instructions. I installed ghost to the root of my domain so in normal operation someone would go to example.com and it'll show them my blog. Well that's what I'd hoped.
However now when I've come to load Ghost in my browser I discover because I'm accessing it the way I am, and that Ghost doesn't do server configuration, I seem to need an htaccess file to be able to make the site reachable.
So, I have created this htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ http://127.0.0.1:65515/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:65515/$1 [P,L]
</IfModule>
With that saved to the root, if I go to example.com/ghost (the admin panel for Ghost) it works. Nothing wrong there, looks great. If however I try and visit the root, ie goto example.com, instead of showing me the index, it shows me index.js - that is, it literally loads the contents of Ghost's index.js file and displays it instead of parsing it and displaying the main index of the website.
IF however I go to www.example.com then it all works. So whatever the problem is it's because I'm not using www. in the domain.
I would prefer it to work both with or without the www in the URL though. I did try adding some solutions to redirect non-www requests to www.example.com to th ehtaccess but for some reason it still doesn't work (as in if I type example.com it doesn't redirect me to www.example.com).
I think maybe you should be using mod_proxy rather than mod_rewrite. At least, that's what I've used in the past. Apache will catch requests coming in on port 80 and then redirect them to port 65515 where your node server is listening.
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Like this:
ProxyPass / http://localhost:65515/
I figured it out. Wasn't anything to do with htaccess, or Ghost or nodejs or anything like it. No, instead the problem was the sodding server was caching the website. I discovered the setting to delete the cache and it all started working fine, so, this is now solved.

htaccess rewriterule for directory is changing the url in a undesireable way

EDIT
After a comment from Seth below, and heading to a helpful apache page here, I have found that VirtualHosts are the way to go for the following issue.
/edit
--ORIGINAL POST--
First, a little background on file setup. I am running a LAMP server that hosts multiple domains. I have staging and live sites on this server, under different directories under the web root.
examples
/webroot/live/site1/[public files]
/webroot/live/site2/[public files]
/webroot/stage/site1/[public files]
/webroot/stage/site2/[public files]
The domains for each of these go to the IP of the server, which points at the webroot directory. I have an .htaccess file there to load the appropriate content based on the http_host.
examples
RewriteCond %{HTTP_HOST} ^www.site1-live.com [NC]
RewriteRule ^(.*)$ /live/site1/$1 [PT,L,QSA]
RewriteCond %{HTTP_HOST} ^www.site1-stage.com [NC]
RewriteRule ^(.*)$ /stage/site1/$1 [PT,L,QSA]
These work great for hitting the home page and any of the internal pages, even with the specific pages being like site1-live.com/view/123. Each site's htaccess handles those.
My issue (sorry it took so long to get here):
When I head to any subdirectory within a site, like www.site1-live.com/rss, the content loads just fine, but the URL changes to something like the following
http://www.site1-live.com/live/site1/rss/
Essentially showing the path from the webroot to the files.
How can I avoid this? I obviously want the url to remain www.site1-live.com/rss. Do I need an htaccess file inside the rss directory to block this somehow?
Thanks in advance!
replace ^www with ^(.*)
then have the whole url in the second line www.yourdomain.com/live/...
Doug,
why do you need the QSA flag?
Anyway, what is happening to you is that mod_index (or whatever is serving you directories) is redirecting you www.site1-live.com/rss (without the ending /) to the equivalent URL with the ending /.
If you don't use mod_alias or something list that on the rewritten URLs, removing the PT should work as you expect.

Resources