making a folder to become a root folder via htaccess - php

got myself in a little dev jam... wondering if any can help... it has to do with .htaccess , modifying url's...
i got an example...
foobar.com/foo/index2.php
how can i get the url to always point to foobar.com/index2.php
(so that "foo" becomes the root folder )
i read it has to to with the .htaccess and server permissions...
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
kind of a newbee, can someone point me to the right direction...

In your root .htaccess (a level above foo) place this rule:
RewriteEngine on
RewriteRule ^((?!foo/).*)$ /foo/$1 [L,NC]

i'm not really good in htaccess but i have some manual solution for this ... it's rewriting the link to make the user if he go this link which you can identify it as subfolder he will open the following link ... here's an example
RewriteEngine on
RewriteCond %{REQUEST_URI} folder/subfolder
RewriteRule folder/subfolder http://localhost/website/file2.php
or
RewriteCond %{REQUEST_URI} folder/subfolder
RewriteRule folder/subfolder http://localhost/website/folder
not that this will give more secured website as nobody will understand that how it's really working over your website

You don't a actually need mod_rewrite to do this.
Try using mod_redirect instead:
RedirectMatch ^/foo/(.*) /$1

Related

Pointing path from url to directory in .htaccess

Currently I'm using this .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.org$
RewriteCond %{REQUEST_URI} !production/
RewriteRule (.*) /production/$1 [L]
so if i'm hit mydomain.org or mydomain.org/* it will pointing to public_html/production folder
But now i need exceptional rules to point mydomain.org/api to folder public_html/api.
How to achieve this?
just for your reference...
try this in your localhost. make one project folder then create one .htaccess file in your root directory
write below code in .htaccess file
RewriteEngine On
RewriteRule ^game/([0-9]+)? http://triviamaker.com [R=301,L]
now run this,
localhost/your_Project_Folder_Name/game/_any_number/ ... when you run this url you will automatically redirect to another url which is set in .access file
I hope you understand this thank you

Rewriting the URL's of 2 subfolders in a /public (root document) folder

Currently I have a "public" folder being root directory where all my frontend code goes.
But since my public folder contains both the website content and app content, I would like to split it more up.
So my idea was:
.htaccess (rewriting public url)
src (php backend code)
public/
web/
index.php
app/
account.php
So my problem is, I can easily go to the pages like this:
http://example.com/web/index.php
http://example.com/app/account.php
but I would like to remove "web" and "app" from the URL's...
http://example.com/index.php
http://example.com/account.php
How can I do that using .htaccess? I'm running apache 2.4.
EDIT
An example of what I've tried:
RewriteEngine On
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1
Probably you need to tune up your requests. But a first approach is:
First, make sure that you have mod rewrite enabled.
Second, write the following in the .htaccess
RewriteEngine On
RewriteRule ^[/]?index.php$ /web/index.php [NS,NC,L]
RewriteRule ^[/]?account.php$ /app/account.php [NS,NC,L]
You will need to read the documentation to make other rules. You can read it here
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Also pay special attention on the flags. Read the documentation to set the ones that applies to your situation.
I've tried so many different things now, but I finally figured it out.
RewriteEngine on
RewriteBase /
# Rewrite every web request
RewriteCond "%{DOCUMENT_ROOT}/web/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/web/$1"
# Rewrite every app request
RewriteCond "%{DOCUMENT_ROOT}/app/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/app/$1"
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works when having a setup:
public/ (root folder)<br>
web/ (contains website frontend)<br>
app/ (contains app frontend)<br>
.htaccess (contains the code above)

htaccess change .html to .php from all files inside folder

so i have this folder inside my site.. and i had to change all file types from html to php but inside this static html i have links to this files using html as extension... the thing is that i have php now.
is it possible to write htaccess rule for this folder so any link to .html will become .php?
www.site.com/folder1/folder2/something.php
inside something.php i have a link to: www.site.com/folder1/folder2/somethingelse.html (but its actually .php now) and i have also hundred of files that have been change to php
i have tried lots of rules but im really noob with them, last one i have tried was
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.net
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://www.website.net/folder1/folder2/$1 [R=301,L]
RewriteRule ^(.+)\.html$ http://www.website.net/folder1/folder2/$1.php [R=301,L]
but it doesn't work!
please help!
UPDATE: MICHAELOKGB answer was almost ok! the only thing left to do was to set as rewritebase the actual folder: "/folder1/folder2/" and its working like a charm! thanks guys!!
Use this. I tested it and it works.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} folder1/folder2/(.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} folder1/folder2/(.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
Edit: Forgot to add specific folder only. If you remove /folder1/folder2/ it will apply to whole site.
Edit2: So by response, it looks like you should change rewrite base from / (whole site) to praticular folder. Its weird, as I tested it for specific folder and it worked, but anyway:
RewriteBase /folder1/folder2

How to set default homepage from another folder in .HTACCESS file?

I created a site and make its URL seo friendly like www.mywebsite.com/home.
but actual homepage is in my /SomeFolder/index.php which is rewrite its url to /home.
Now i want to set /home url as a default page in htaccess file. But when i set this.
DirectoryIndex /home
I got the 500 error. One way is to give the redirection in index.php so it will redirect me to the /home page. but i think its a weird method.
Is there any other way??
Thanks
Remove that DirectoryIndex line.
You'll need this rule in your DocumentRoot/.htaccess:
RewriteEngine on
RewriteRule ^home/?$ /SomeFolder/index.php [L,NC]
this best article for create pretty URLs :
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
Try DirectoryIndex home/index.php
use this one , hope this will helpful for you
Options +FollowSymLinks
RewriteEngine on
RewriteRule home(.*)\$ index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/somefolder
RewriteRule ^(.*)$ /somefolder/$1 [L]
src: https://www.siteground.com/kb/how_to_change_my_document_root_folder_using_an_htaccess_file/
Although I have entered an issue so I have an open question, which might be worth monitoring. I will update if I figure it out myself which I aim to do.
.htaccess make sub dir home dir but force www and https whilst maintaining links

URL Shortening in Apache 2

I have a site with homepage as /var/www/mysite/index.html
Now I want to direct www.mysite.com/x2312d to /var/www/mysite/app/pass.php?id=x2312d
How do I do this?
Where should I create .htaccess file and what should be the contents of it?
in your root /var/www
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ /app/pass.php?id=$1 [NC,L]
put the .htaccess file in /var/www/mysite/app/. I'm assuming your docroot is /var/www/mysite/
make sure apache 2 has the rewrite module enabled
.htaccess:
RewriteEngine On
#allows you to still access static items in this dir
RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g)$
#sent it all to pass script
RewriteRule (.*)$ pass.php [L]
I like this method, assuming pass is some sort of controller. My controller for a rest api parses the /x23123d manually from the $_SERVER global var, but you could use a different rewriterule to have it be in the $_GET/$_REQUEST['id'] global var.
something like this:
RewriteRule ^app/(.+)$ app/pass.php?id=$1 [PT,L,QSA]
Good References: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
edit: almost forgot, don't forget to handle trailing slashes too.

Categories