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

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

Related

removing /index from the url on htaccess

My current urls look like this [mysite].com/index
I removed .php's with the following .htaccess code but i can not remove the index from the url.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
How can i remove 'index' from the url?
EDIT
I found why it does not work because of windows hostings does not support htaccess method.
Use
DirectoryIndex index.php
Now apache will treat index.php as directory index and you don't have to type index or index.php in url bar.
Actually, removing the page name for good it's not a good practice.
It may cause errors, and it's not good for SEO.
I strong suggest you to use friendly URL to remove the PHP extension(the way you're doing right now) or put some other name more friendly.
Exemple:
RewriteEngine On
RewriteRule ^home/?$ /my_new_index.php [NC,L]
RewriteRule ^new-order/?$ /new_order_from_client.php [NC,L]
It's the best practice!

making a folder to become a root folder via htaccess

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

PHP -- Using .htacess rewrite rules to hide folder name in URL

I'm doing a small php personal project for fun and I need help with my rewrite rules. My goal is to hide completely a folder name in my URL.
Here's what I got already :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my-app/
RewriteRule ^(.*)$ /my-app/$1 [L]
</IfModule>
This is working fine, I don't need to specify my-app folder in the URL but when I redirect to another page the folder name is displayed again. What can I do to specify that when my-app folder is requested, to hide it from the URL. Thanks in advance.
Try adding this rule to your htaccess file:
RewriteCond %{THE_REQUEST} \ /+my-app/
RewriteRule ^my-app/(.*)$ /$1 [L,R=301]
This will redirect any direct request for anything in /my-app/ to a URL with it removed.

htaccess rewriting condition/rule for subdomains

I would like to rewrite all index files like /index in my subdomain as well as in my domain. Up to this point there was no need to use a subdomain. Now the problem is a rewrite rule from the htaccess file. This rewrites the URL as in the given code below:
RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L]
Means that in the domain all index files will be rewritten. This works well but not in the subdomain. Now I thought I can simply add a condition for my subdomain like:
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$
RewriteCond %{HTTP_HOST} !^www\.
but this fortunately does not work.
To give further information here is what a URL looks like:
http://www.example.com/index
and the subdomain:
http://subdomain.example.com/index
With the code above the URL will be:
http://www.example.com/
and the subdomain:
http://subdomain.example.com/index
It would be great if someone could help me out.
Thanks alot.
UPDATE:
To give further information I need to explain how things work.
In the root dir there is the folder for the subdomain.
--> /index.php
--> /folderA
--> /subdomain
/root --> /folderB
--> /index.php
The URL´s look like that:
http://www.example.com/subdomain/folderA/index
and
http://subdomain.example.com/folderA/index
I do use clean URL´s so that it is just index and not index.php etc.
Default settings already hide index.php when calling a page. The problem will be when I will change the languages what means folderA and folderB. Therefor I read out the basename of the file and use header function to redirect to the right dir. The main problem is the subdomain. In the domain it works well. Just when I have a index page from a folder with the URL:
http://subdomain.example.com/folderA/(index will be hidden)
and will read out the basename (=>index) and will header to:
http://subdomain.example.com/folderB/(index will be hidden)
it will be caused a problem. The URL will be rewritten in a wrong way. Or another simple example:
Having a logo button on all pages in root/subdomain/folderA/
This button is a link with just: .... The page URL will be: subdomain.example.com/folderA/filexy when clicking that link the URL will be rewritten to www.example.com/subdomain/folderA/
Have your rules like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# do nothing if subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule - [L]
RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L]
PS: Though I suggest taking a look at DirectoryIndex directive and then you can replace above code with this line:
DirectoryIndex index.htm index.html index.php
Try this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
RewriteRule ^/index\.(htm|html|php)$ http://%{HTTP_HOST}/$1 [NC,R=301,L]
</IfModule>

redirect php script with .htaccess from upper folder

I'm trying to do a 301 redirect with mod_rewrite on a PHP script from an upper directory on my website. The URL I want to redirect looks like this (obviously the querystring is always different).
http://www.foo.com/bar/script.php?variable1=bar&variable2=foo
And I want to redirect script.php? to the lower lever on my site, like this:
http://www.foo.com/script.php?variable1=bar&variaable2=foo
Can anyone help with this?
Thanks,
Matt
For a vhost config (which i prefer to a htaccess), it would be
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/bar/script.php
RewriteRule $ /index.php [R=301,NC]
for htaccess it should be
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/bar/script.php
RewriteRule $ /index.php [R=301,NC]
You could try something like this:
RewriteEngine On
RewriteRule ^bar/script\.php$ http://%{HTTP_HOST}/script.php [QSA,L,R=301]

Categories