Understanding subdomain url mod-rewrite - php

I need to understand url mod-rewrite but couldn't get the basic at all.
I want to change www.domain.com/folder/page.php?id=1 to 1.domain.com
I created a wildcard subdomain (*.domain.com) and the problem is, where should I point the subdomain to? is it public_html/ or public_html/folder/ ?
Which htaccess file should I modify? is it in public_html/ or public_html/folder/ ?
What's the link to the page.php? is it a href="1.domain.com" or a href="www.domain.com/folder/page.php?id=1"
Thank you very much :D really appreciate your help :D
EDIT :
.htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /file.php?item=%2 [QSA,nc]
I typed 1.domain.com but it's showing no server found.

That strongly depends on your setup. Point it to the same directory where you'll put the .htaccess in. Most of the time a subdomain points to an extra directory i.e. public_html/1/ where you can put the .htaccess in and separate it's logic from the rest of your website.
That one where the subdomain points to, see answer 1. :)
The link will be a href="1.domain.com"

Related

.htaccess mapping www & non-www to folder and maintain www

I am no .htaccess expert and extremely rarely use it. I went through several solutions on Net and SO but no luck still. So I have one IP says 1.2.3.4 that has sub folder of my wordpress application says ABC in the /var/www/html/. DNS has been setup so that www.mydomain.com and mydomain.com point to IP 1.2.3.4.
I have my content in .htaccess file as below and it only supports mydomain.com. The content is displayed as expected except when I put www.mydomain.com it displays Linux home page.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteCond %{REQUEST_URI} !^/ABC/
RewriteRule (.*) /ABC/$1
When I put www.mydomain.com the browser automatically change the URL to mydomain.com.
Apart from that, I would also like to have all domain with www. Means request from mydomain.com will be displayed as www.mydomain.com. Later I will just put www.mydomain.com in the URL config of my wordpress. Some helps are appreciated.
Switch back to your old .htaccess code. I got it more clear after your last update. You have wordpress files in "public_html/ABC" but need your website url as www.example.com. Let me explain step by step.
Paste your old code in .htaccess.
copy (do not move) the index.php and .htaccess file from the /ABC folder to the "public_html" of your site.
Find one rule in "public_html/index.php" as "require( dirname( FILE ) . '/wp-blog-header.php' );" and replace it with "require( dirname( FILE ) . '/ABC/wp-blog-header.php' );" (do not edit the index.php file in the folder /ABC!!)
change ONLY the "Site Address (URL)" in the general settings (Settings -> General) to "http://www.example.com".
So finally I managed to get what I want to be working with below solution. Hopefully this will help somebody out there:
RewriteEngine on
RewriteBase /
# Whatever request coming to the server, add www to it
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Map the www URL to the subfolder in root directory
RewriteCond %{HTTP_HOST} www\.myurl\.com$
RewriteCond %{REQUEST_URI} !^/thefolder/
RewriteRule (.*) /thefolder/$1
For Wordpress part, I have put codes in wp-config.php as below:
define('WP_HOME','http://www.myurl.com');
define('WP_SITEURL','http://www.myurl.com');
Thank you. Cheers.
UPDATE
I have just realized that after the changes above, I could not able to login to my WP admin page. This is due to wrong redirect. It looks something like http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1
When I remove the thefolder from URL it redirects to expected page. Can somebody advice on this?

Use htaccess in a subdirectory to access a specifice file with a php extention

Presently I have htaccess in domain.com to access file.php - Works great!
RewriteOptions inherit
RewriteEngine on
Options -Indexes
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/file\.php" [R=301,L]
It looks like this when entered: www.domain.com/file.php
Ok, what I'm not able to figure is how to place something similar in a subdirectory. Example result would be: www.domain.com/subfolder/file.php.
The htaccess file cannot reside in www.domain.com like the htaccess example above. It (htaccess) has to be in the subdirectory.
I wish I knew how to do this. I searched the web and tried several different ideas, but my experience with apache and htaccess is limited.
Greatly appreciate any help or direction to existing examples that have already answered my question.

How to redirect website by htaccess HTTP_HOST

I have a problem with my website. I have a domain abc-xyz.com and I was uploaded source into a directory like name /folder . Ok, I can visit website by abc-xyz.com/folder . But problem detected. I need rewrite URL! Because root directory running another website, i want to rewrite URL for childrent directory /folder . And "http://abc-xyz.com/folde/r" is a string.
So i was try:
RewriteBase /
RewriteCond %{HTTP_HOST} !^abc-xyz\.com$ [NC]
RewriteRule ^(.*)$ http://abc-xyz.com/test/folder/$1 [R=301,L]
but it not workly. when i visit http://abc-xyz.com/test/folder/ . It direct to abc-xyz.com (and it's a another website).
Thanks for read, and please help me. Sorry because my English very bad :(
From what I understand, you want to redirect all requests into a subdirectory of a site?
e.g.
http://abc-xyz.com/something.txt goes to http://abc-xyz.com/folder/something.txt
http://abc-xyz.com/directory/file.txt goes to http://abc-xyz.com/folder/directory/file.txt ?
Does the solution from this page work for you (which I've edited slightly below)?
https://stackoverflow.com/a/3414030/3944304
RewriteEngine on
RewriteRule !^folder($|/) http://abc-xyz.com/folder/%{REQUEST_URI} [L,R=301]

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

mod_rewrite redirect from any page to home page

I need to redirect if someone goes to any folder e.g. http://site.com/images/ or http://site.com/images to http://site.com.
Unless he goes to file e.g. http://site.com/images/index.php in this case it does not redirect
now i use
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
but i think its not perfect because e.g. if someone comes on http://www.site.com it does not work.
Keep in mind due to server configuration i need to put .htaccess in every folder.
I believe that what you are looking for is the -f flag:
RewriteCond %{REQUEST_FILENAME} !-f
See http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you are only looking at stopping someone from getting a list of the files you have in the folder, you may want to consider instead adding:
Options -Indexes
This will tell apache not to display a directory list if no index page is present in the given directory.
See http://httpd.apache.org/docs/2.2/mod/core.html#options
The last thing to note, if you are not familiar with how .htaccess works, apache scans the current folder and any of its parent folders - you should be okay with only adding a single .htaccess file in your web root for http://site.com. This has been covered in another question here.

Categories