mod_rewrite subdomain and clen url in codeigniter - php

I suppose my domain is "www.domain.com"
and I have a file system
public_html
|----ds
|--application
|--cache
|--controller
...
|--css
|--image
|--system
|--index.php
|----zs
|----fd
public_html is root directory.
I coding in .htacess with have condition this ::
1) ds Directory is subdomain. when URL is ds.domain.com. It mapping filepath in www.domain.com/ds/index.php/
2) I want to clean URL form www.domain.com/ds/index.php/translate/paintext (example full url) to ds.domain.com/translate/paintext (clean url)
how to coding in .htacess ?
/********************************************************************/
I put the .htacess in /ds folder and try to coding :
#subdomain to subdirectory
RewriteCond %{HTTP_HOST} ^www.domain.com/ds/
RewriteRule ^(.*)$ http://ds.domain.com/$1 [L,R=301]
#clean URL from codeigniter
RewriteCond $1 !^(index\.php|lib|css|image)
RewriteRule ^(.*)$ index.php/$1 [L]
What is wrong??

At least one thing that is wrong is your HTTP_HOST. That variable is the hostname, and hostnames contain no URL path info in them. So www.domain.com/ds/ will never exist as a hostname. Change it to:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^ds/(.*)$ http://ds.domain.com/$1 [L,R=301]
The second issue looks fine. But if you want to redirect a request for /index.php to a new URL that doesn't have the "index.php" part, you need a new rule to do that:
RewriteCond %{THE_REQUEST} \ /+index\.php/
RewriteRule ^index.php/(.*)$ /$1 [L,R=301]

Related

Accessing the index file in public_html from SubDir

I got a subDir with a '.htaccess' file, what I want is when this file recieves a request, the index in the root execute without any change of location in the file ..
Sub dir location : 'public_html/subdomains/news'
I wrote the '.htaccess' file as following
RewriteEngine on
RewriteCond %{HTTP_HOST} ^news\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.news\.example\.com$
RewriteRule ^/?$ ../../index.php?url=news [L,QSA,NC]
but didnt work
As this subDir belongs to a subDomain, I dont want its address changed , I want the user to access the location address:
http://www.example.com/news
Through invoking the address:
http://news.example.com
without them seeing it
You need to make a sub-domain and point that sub-domain to folder.
like news.exmaple.com will be pointed to public_html/news (where news is the folder name)
No need to do any thing in .htaccess file.
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.s\.co
RewriteCond %{HTTP_HOST} ^(.*)\.example\.s\.co
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]

Dynamic subdomain pages with .htaccess or PHP

I have my directory like this:
/index.php (website main page)
/sites
site1
site2
site3
when I go to site1.example.com, i want it to show example.com/sites/site1/index.php
when I go to site1.example.com/page1, i want it to show the file example.com/sites/site1/page1.php
how can i do that? thanks!
I assume all your subdomains point to the same place as the main domain, so subdomain.example.com and example.com point to the same place.
Try something like the following in the .htaccess file in the root of the main site:
RewriteEngine On
# Exclude requests for the www subdomain
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ - [L]
# Requests for the site root
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule ^$ sites/%1/index.php [L]
# Requests that are "assumed" to be .php files
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule ^([^.]*)$ sites/%1/$1.php [L]
# All other requests
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule !^sites/ sites/%1%{REQUEST_URI} [L]
This assumes that a valid URL-path does not contain a dot.
%1 is a backreference to the captured group in the CondPattern (ie. the subdomain) and $1 is a backreference to the captured group in the RewriteRule pattern.
However, how do you want to handle non-PHP files and other static resources?
Also, how can I do a 404 page?
You can define a custom error document. For example:
ErrorDocument 404 /errors/404.php
However, you'll need to make an exception at the top of your .htaccess file to prevent this from being rewritten by the directives that follow. For example:
RewriteRule ^errors/ - [L]
You need to:
Setup a Wildcard DNS record
Write a rule for your .htaccess
The rule could look like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/sites/%1/$1 [L,NC,QSA]
That's it.

Redirect all folders to subdomains htaccess

I am trying to redirect every folder from domain.com to it's subdomain.
Examples:
domain.com/a -> a.domain.com
domain.com/b -> b.domain.com
domain.com/about -> about.domain.com
So basically, every folder redirected to it's subdomain. For the first example, folder a contains index.html. When I browser to domain.com/a it should redirect the url to a.domain.com but use the index.html file from the a folder.
My current htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteRule ^(/)?$ %1/index.html [L]
What works
For now, if I navigate to test.domain.com, it's working, it's taking the index.html file from the test folder. If I go to domain.com/test it's still working, but I want to disable this. I want to redirect domain.com/test to test.domain.com as well, but still use the index.html files from the test folder.
Another thing that is in the htacces file is to redirect www to non-www.
Any suggestions would be appreciated.
EDIT:
Forgot to mention that I've tried to add this line:
RedirectMatch 301 ^/test/(.*)$ http://test.domain.com/$1
It's doing the redirection from domain.com/test to test.domain.com/index.html, but it's not loading the page right, I get an error (Page isn't redirecting properly).
keep like
RedirectMatch 301 ^{RELATIVE PATH}$ {ABSOLUTE_PATH}

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

Pointing the website http root to a sub folder with htaccess?

How can I use RewriteRule to point the website http root to a sub folder?
I always use the rule below but now after upgrading to the latest wamp server, it does not working anymore. Why?
RewriteRule ^/?$ local/applications/bin/oldsite/index.htm [L,QSA]
I want to point, for instance,
http://mysite.com/ to http://mysite.com/local/applications/bin/oldsite/index.htm
or in localhost wamp server, it should be like this,
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
Any ideas?
And also,
http://localhost/mysite/contact.htm to
http://localhost/mysite/local/applications/bin/oldsite/contact.htm
http://localhost/mysite/about.htm to
http://localhost/mysite/local/applications/bin/oldsite/about.htm
EDIT
I managed to point the other pages to the specific locations with the rule below,
RewriteRule ^([a-zA-Z0-9\-]+)\.htm/?$ local/applications/bin/oldsite/$1.htm [L,QSA]
But still I cannot point
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
UPDATED
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#### localhost sets of rules
# No target file, go to index.php
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/?$
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteRule . mysite/local/applications/bin/oldsite/index.htm [R=301,L]
# Target file, go to file
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/([^\.]+)\.htm [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . mysite/local/applications/bin/oldsite/%1.htm [R=301,L]
#### Live site set of rules
RewriteCond %{HTTP_HOST} (?:www\.)?mysite\.com [NC]
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule . local/applications/bin/oldsite/index.htm [R=301,L]
The first sets of rules are for the local host and redirect permanently:
http://localhost/mysite/filename.htm
To:
http://localhost/mysite/local/applications/bin/oldsite/filename.htm
When filename.htm is not present in the incoming URL, filename.htm in the substitution URL is always index.htm
The string mysite is assumed to be a fixed string while filename is assumed to be a variable.
The second rule-set is for the web server and redirects permanently only
http://mysite.com/
To:
http://mysite.com/local/applications/bin/oldsite/index.htm
In the last case, if the incoming URL holds any additional path the rule is not applied.
For silent mapping, remove R=301 from [R=301,L].
Copy the above sets of rules into both .htaccess files in the local and remote root directories.
This might get you started (untested):
Check in a condition whether the request does not point to a particular subfolder. If so, redirect the request to a subfolder. You can use in the target if you want. Instead of the HTTP Status 301 you can use another one if more appropriate.
RewriteCond %{REQUEST_URI} !^/(local/applications/bin/oldsite/.*)$
RewriteRule ^(.*)$ http....n/subfolder/%1 [R=301,L]
It seems like you want to make the "oldsite" directory your Document Root - right?

Categories