index.php in subfolder with .htaccess - php

I have my index.php inside a subfolder which is inside another subfolder, etc. So, my hosting company doesn't let me modify the httpd-vhosts.config so the only way I have to indicate that I want to run my web from /subfolder1/subfolder2.... is the .htaccess.
I've searched a lot in stackoverflow, and all the stuff didn't work. Basically, I have these folders :
web
instances
myweb
public
root
Inside root, there is the index.php. So , when I enter at: www.mydomain.com.mialias.net, I want to go to index.php and run my website,
I've tried this :
RewriteEngine on
RewriteCond % ^midomain.com.mialias.net\.com$
RewriteRule (.*) http://www.midomain.com.mialias.net/$1 [R=301,L]
RewriteRule ^$ instances/myweb/public/root [L]
And it didn't work because the URL is like this : www.midomain.com.mialias.net/instances/myweb/public/root , which is wrong because I need to have my URL like : www.midomain.com.mialias.net/ in order to insert arguments on the URL, like : www.midomain.com.mialias.net/myaccount, and at the moment this is impossible because I have the URL as I said.
Does anyone know how to do it? Thank you very much!

Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.mialias.net\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/instances/myweb/public/root
RewriteRule ^(.*)$ /instances/myweb/public/root/ [L]

Related

.htaccess Redirect any file in directory to index.php of same directory

I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]

Load Multiple Urls as One Page

Not 100% sure how but I know its simple .htaccess but I have no idea what to do.
Basically I want it to load a index.php file no matter what url they go to in the folder containing the index.php for example:
http://website.com/folder/thisisntafile.php will load: index.php in the folder named folder. This will happen for whatever /folder/file.php is loaded.
Thanks!
Try this :
RewriteEngine on
#Rewrite "folder/file"
#don't rewrite "folder/index.php"
Rewritecond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} ^/([^/]+)/[^.]+(\.php|html)?$
RewriteRule ^ /%1/index.php [NC,L]
This rewrites
http://example.com/foo/bar.php
to
http://example.com/foo/index.php

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Create a Subdomain in godaddy hosting using PHP

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting?
e.g:
jon.mywebsite.com
jessie.mywebsite.com
etc
you need to create a A record to serve all your subdomains
*.your-site.com IN A YOUR-IP-ADDRESS
then you need to create a .htaccess file and
RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}
this will ignore images (SEO friendly URLs).
Now you can redirect your users to $userName.your-site.com
Alternatively try this:
setup your application so your users goes to
your-site.com/user
your .htaccess should look like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$ index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*) index.php?username=%1
so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. (in this case usernames are limited to a-z characters)
I dont think with PHP Code you can but with the help of .htaccess i think it is possible.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]
RewriteRule ^(jon|jessie)/ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]
You can make subdomain much easier using control panel, why to do it the hard way ?
This is an old question, but in case anyone is looking for the answer. This is specific to GoDaddy, but other Hosts may do something similar to this.
After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account.
Go to Hosted Domains.
Under Hosted Domains you will see a root domain table column.
You will also see a Subdomains table column with an "Add" link.
This link is for adding subdomains to a root domain.
Type in/enter the name of the subdomain folder.
Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. After that is done then you can add anything you want in that subdomain folder/site.

.htaccess to redirect parts of a URL to local files

I have a custom web app and the file structure works like this:
/apps/calendar/frontend/index.php
/apps/calendar/frontend/view/index.php
/apps/calendar/backend/index.php
/apps/calendar/backend/edit/index.php
/apps/calendar/backend/add/index.php
/apps/calendar/backend/view/index.php
I'm trying to write a .htaccess file to help redirect the files so they cant see the 'real' path.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^([^/\.]+)/(.*)/(.*)($|/$) /apps/$1/frontend/$2/$3 [NC,L]
RewriteRule ^([^/\.]+)/(.*)($|/$) /apps/$1/frontend/$2 [NC,L]
RewriteRule ^([^/\.]+)($|/$) /apps/$1/frontend/ [NC,L]
When I visit localhost/calendar it should map redirect to /apps/calendar/frontend/index.php. But when I visit localhost/calendar/add it gives me a 301 (permanent move) then shows the full page of localhost/apps/calendar/frontend/add/index.php in the console. Anyone got any ideas why this would happen? Or a better way around this? The apps might have heaps of sub-directories so, I'm not particularly keen on having a rule for ever subdirectory combination.
As you can see also I have a /admin path, which would load the /backend/ parts of the app. I would assuming I can do the similar code with the prefix of /admin?
This question might also be of your interest: Create blog post links similar to a folder structure.
Given that your .htaccess is located on the root folder of your domain /home/youraccount/public_html/.htaccess, it would look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(admin|apps) [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^([^/]+)/?(|.*)$ /apps/$1/frontend/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/apps [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^admin/([^/]+)/?(|.*)$ /apps/$1/backend/$2 [NC,L]
Let's say the user access:
http://domain.com/calendar
http://domain.com/calendar/
http://domain.com/calendar/add
All the above would redirect to
/apps/calendar/frontend/index.php
/apps/calendar/frontend/index.php/
/apps/calendar/frontend/index.php/add
And the if the user access:
http://domain.com/calendar/admin
http://domain.com/calendar/admin/
http://domain.com/calendar/admin/add
It would go to:
/apps/calendar/backend/index.php
/apps/calendar/backend/index.php/
/apps/calendar/backend/index.php/add
So it would make index.php your controller for each end:
/apps/calendar/frontend/index.php
/apps/calendar/backend/index.php

Categories