Rewrite any sub-domain and all files to a single directory - .htaccess

When a user goes to any subdomain of my website, for example test.domain.com or even sdjfhdihdfig.domain.com, I want the page of http://www.domain.com/directory to be shown (this directory will be the same regardless of the subdomain the user goes to).
If the user heads over to any page of any subdomain (for example, test.domain.com/apage.html), I want the page of http://www.domain.com/directory/[PAGE] to be shown (in this case the page www.domain.com/directory/apage.html would be shown, but of course this would change depending on what came after the subdomain).
I found something that seems to solve this problem, however, it redirects the user rather than keeping the subdomain as it is in the URL bar. If I went to test.domain.com/test, I want this URL to stay as it is, but the page located at www.domain.com/directory/test should be served instead.
Does anyone have any ideas about how I could do this with htaccess? Thanks in advance.

Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www)[^.]+\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/directory/$1 [R,L]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
This assumes that mod_rewrite is both installed and activated for .htaccess files.
If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

I managed to figure out the solution to my problem eventually.
This is my .htaccess document:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)?(.+)$
RewriteRule ^$ s/$1/ [L]
When someone goes to example.domain.com, the page at http://www.domain.com/s/ is displayed. When they go to a page like example.domain.com/s/page.html, the page at http://www.domain.com/s/page.html is displayed instead.
If the user goes to www.domain.com, this is not treated as a subdomain and instead my main website homepage is displayed.
I've set up a quick script so if someone decides to go to example.domain.com/page.html, instead of showing the page from www.domain.com/page.html, they are redirected to a 404 page on my server. The script (PHP) for how to do this is below:
if(substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.' AND substr($_SERVER['REQUEST_URI'], 0, 3) != '/s/' AND $_SERVER['REQUEST_URI'] != '' AND $_SERVER['REQUEST_URI'] != '/') {
header("Location: http://www.domain.com/404.php");
}
I got the htaccess file from htaccess get domain name from %{HTTP_HOST} into variable, minus the www and just adapted it a little bit.
I hope this helps someone in the future!

Related

.htaccess RewriteRule : not all rules work on Ionos [duplicate]

Start by explaining what I'm trying to do:
I've got different pages on my website. Some pages have the same templates so I create one page with parameters to adapt my page: Parameters are called pageview and lang the URL looks like this:
http://mywebsite/home/en <- http://mywebsite/index.php?pageview=home&lang=en
http://mywebsite/page2/fr <- http://mywebsite/index.php?pageview=page2&lang=fr
for example. To dot that, I use the famous .htaccess file and it module rewrite_module for Apache.
I've got also a contact page with a different template. It URL looks like this and here there is only one parameter:
http://mywebsite/contact/fr <- http://mywebsite/contact.php?lang=fr
http://mywebsite/contact/en <- http://mywebsite/contact.php?lang=en
Here is my .htaccess code:
RewriteEngine On
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?pageview=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?pageview=$1&lang=$2 [QSA]
The problem is that .htaccess file work for the index.php but not for contact.php
I can fully access to contact.php but the parameter is not detected
Thanks for your help 😀😀 !
EDIT
If I remove index parts to stay only the contact rewriteRule's the problem stay there.
contact.php and index.php are in the root folder
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
It looks like you may have a conflict with MultiViews. If MultiViews is enabled then mod_negotiation will rewrite a request for /contact/fr to /contact.php (without any parameters) before mod_rewrite is able to process the request.
Try disabling MultiViews at the top of your .htaccess file:
Options -MultiViews

htaccess Redirect 301 directory

I've read many things about this, but none seems ok at the moment.
I've to redirect:
http://mysite.it/255-dir-name/
to:
http://mysite.it/forum/255-dir-name/
Also, everything which is in this directory must be redirected to the new path.
I've tried with this, but doesn't work:
RewriteRule ^255-dir-name/(.*)$ /forum/255-dir-name/$1 [R=301,NC,L]
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^255-dir-name(.*) http://example.it/forum/255-dir-name/$1 [R,L]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
Warning! When you test the rewrite, do so in a NEW browser, or fully clear its cache. That is because your previous 301 redirect has been cached by your browser, so your browser does not even request the page from your server anymore. Instead, it requests the previous rewrite directly.
You can redirect the first url to the second url with the following directives in your .htaccess:
RewriteEngine on
RewriteRule ^([0-9]+-[^/]+/?)$ forum/$1 [R,L]
This will redirect any url that starts with one or more numbers, a dash, and then one or more non-slash characters to the forum-url.

How to reflect another site in my site using URL Rewriting?

I think my doubt is simple, but I could not find the answer.
I have a linux server that hosts the domain http://www.DomainOne.com
And I have a second server based in Windows plataform that hosts the domain http://www.DomainTwo.com
What I want to do is: When I access the DomainOne.com, it shows all contents of DomainTwo.com, but WITHOUT redirecting the page.
In this case, if I access DomainOne.com/defaultPage.asp?Attribute=test&foo=bar
the URL in browser will be exatly this url, but, the URL showed to the user will be
DomainTwo.com/defaultPage.asp?Attribute=test&foo=bar
Is it possible?
I can't move my pages from DomainTwo to DomainOne because my pages is based in ASP, that works just in Windows. Than, I think that the only solution is to reflect my URL in the original one.
Thanks Guys.
On DomainOne.com enable mod_rewrite, mod_proxy and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?DomainOne\.com$ [NC]
RewriteRule ^ http://DomainOne.com%{REQUEST_URI} [P,L]

How can I redirect to another file but keep index.php access

I will try to explain this in the simplest terms I can think to avoid confusing anyone.
I have a site that I've moved to a subdirectory, it will go away soon but for needs access. In the root I have a wordpress install with an index.php file.
Basically for the time being I need this:
User types www.site.com they get taken to www.site.com/comingsoon.php
User types www.site.com/index.php they get taken to that actual URL (no redirect)
This is the only line I have in my .HTACCESS:
DirectoryIndex comingsoon.php
but that doesn't seem to work as if they go www.site.com/index.php (by typing that explicitly) they still get redirected to the coming soon page.
I can't rename the index.php from wordpress.
Any help is appreciated.
Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^/?$ /comingsoon.php [L]
This will only internally redirect www.site.com or www.site.com/ to /comingsoon.php. But if you type www.site.com/index.php it should not do any redirection.

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources