.htaccess and url rewriting challenge - .htaccess

My problem is very simple.
I have several folders in my root:
[folder1]
[folder2]
[folder3]
And my domain is something like: http://johndoe.net
I keep my website in [folder1], so basically I access it by typing http://johndoe.net/folder1
My goal is this:
I want to type http://johndoe.net and I get the content of http://johndoe.net/folder1 but the address has to remain http://johndoe.net
I want to type http://johndoe.net/folder1 and see my website, but my address has to change to http://johndoe.net
Seems easy enough, but I failed finding the solution after several hours of searching.
So far, the only thing I achieved is redirecting from http://johndoe.net to http://johndoe.net/folder1 by placing this bit of code in my .htaccess in my root:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "http\:\/\/johndoe\.net\/folder1\/" [R=301,L]
When I type http://johndoe.net, I get http://johndoe.net/folder1 in my address bar
but what I need is my address to remain http://johndoe.net
Could anyone help me with this?

You're going to use two .htaccess files to solve this problem. Here's what you put in them, and where they go.
File #1. This goes in your base folder (the one that contains [folder1],[folder2], etc)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((www\.)?johndoe.net)$
RewriteRule ^(.*)$ /folder1/$1 [L]
File #2. This goes in [folder1]
RewriteEngine Off

Try something like this
RewriteRule ^(.*)$ /folder1/$1 [R=301,L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "\/folder1\/" [L]
you need internal rewrite, So it's enough to rewrite / to /folder1/

Related

Redirect page from one domain to another domain page

I seearched a little bit but seems like I cant find proper answer for my problem. I have 4 domains and I need to redirect 3 domains (and some of their pages) to new one.
https://www.domain1.com/ to https://www.domain4.com/music/domain1/
https://www.domain2.com/ to https://www.domain4.com/music/domain2/
https://www.domain3.com/ to https://www.domain4.com/music/domain3/
and i need it for some pages also, for example:
https://www.domain1.com/about.php to https://www.domain4.com/domain1/about/
Can someone help me how to do this in htaccess?
Thanks in advance
Could you please try following, written as per shown samples. After placing these Rules into your .htaccess file, before testing make sure you clear your browser cache.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain[123])\.com [NC]
RewriteCond %{REQUEST_URI} ^/?$
REwriteRule ^(.*)$ https://www.domain4.com/music/%1/ [NE,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain[123])\.com [NC]
RewriteCond %{REQUEST_URI} ^/(about)\.php/?$
REwriteRule ^(.*)$ https://www.domain4.com/%1/%2/ [NE,L]

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

Need help figuring out .htaccess rules

I'm pretty new to apache, and I need some help with some .htaccess rewrite rules. Here're the current rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
Basically it redirects any requests to mydomain.com\beta\index.php, except if a filename's specified. This works fine for now.
Here's the problem:
I have a subdomain beta.mydomain.com which I don't want these rules to apply to. Any URL on beta.subdomain.com should be treated normally. However, since the .htaccess is at the server root, it's rules are messing with the beta.subdomain.com as well.
I've tried many different combinations of regex, but I can't figure it out. Help!
You need use something like this (to check if your request was related to specific domain/subdomain):
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
20 seconds were missed... But my answer is better ;)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteCond %{HTTP_HOST} !^beta\.subdomain\.com
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
This ought to do the trick!

How can i get my htaccess to work (subdomains)?

I'm sorta a noob at these things but I'm trying to make a simple virtual subdomain with .htaccess. I have wildcard enabled and after lots of digging, this is what I've come up with:
rewriteEngine On
rewriteCond %{HTTP_HOST} !^$
rewriteCond %{HTTP_HOST} !^(www\.)?khpedia\.com$ [NC]
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
rewriteCond %2<->%3 !^(.*)<->\1$ [NC]
rewriteRule ^(.+) /%2/$1 [L]
My directory is setup as
-root
--wiki
----index.php
--test
Right now when I travel to wiki.khpedia.com, I get a page not found. When I travel to wiki.khpedia.com/index.php, it travels to wiki.khpedia.com/wiki/index.php. I am somehow also able to access wiki.khpedia.com/test. If it doesnt seem obvious yet, I want to be able to go to wiki.khpedia.com/index.php and see wiki.khpedia.com/wiki/index.php but not in my address bar. Sorry for the text block and thanks for the help.
This works on the Apache side:
RewriteCond %{HTTP_HOST} !^www\.khpedia\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).khpedia\.com$ [NC]
RewriteRule ^(.*) /%1/$1 [L,QSA]
But you're wiki software might make up it's own mind about redirects / urls.
RewriteCond ^(.*)$ /wiki/$1 [L]
I'm confused about where subdomains come into your question. Could you give some examples of URLs you want to access in the browser and what they should point to at the server?
EDIT | Ah, I see now, Wrikken's answer handles the subdomains correctly :)

Resources