htaccess rewriting condition/rule for subdomains - php

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>

Related

.htaccess URL rewrite rules folder

I have this in my html folder
--html
--.htaccess
--app
--application_name
--public
--index.php
--landing_page
--index.php
As of now, everytime the website url is accessed (www.website.com), the browser opens the landing_page/index.php.
How can I make it that when I access www.website.com/beta, I will be doing a request to html/app/application_name/public/index.php
I have tried adding this to the .htaccess
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
This is the whole .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
But it does not work. Not sure what term to use to search for this kind of problem.
Your root is currently set at /html, and therefore the RewriteBase / is at /, the /beta/ simply does not exist. You could change the RewriteBase /app/application_home/public/ and set the RewriteRule ^beta/?$ index.php [L,NC]. This however no longer give you the access to the landing page, not sure if this is what you want?

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

How do I rewrite the main page?

if there is no index file in the folder how do i rewrite an index file from another folder...so for example the main site has two folders like this
main_folder/no index.php
folder1/index.php
folder2/index.php
what do i have to put in the rewrite rule to make the main page show the index for folder1/index.php
EDIT
got it working with this
RewriteEngine On
RewriteCond %{HTTP_HOST} site.com
RewriteCond %{REQUEST_URI} !folder1/
RewriteRule ^(.*)$ folder1/$1 [L]
Try this:
#in the .htaccess file where there is no exit or in root
Options Indexes #if still fails delete this line
DirectoryIndex /folder1/index.php
Not sure though
edit: for sub levels maybe adding this solves.
RewriteCond %{REQUEST_URI} ^/folder1/
RewriteRule ^(.*)$ http://www.example.org/$1 [L]
do you just want a redirect?
Redirect 301 / /folder1/index.php

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]

how to write a rewriteCond in .htaccess to exclude a subdir and its subdirs

I have the following problem. I have a website and a blog in a subdirectory. Both of them are php. I have a .htaccess file in the root folder and another one in the blog folder. I dont' think is relevant, but the blog script is wordpress.
I added a condition in the root .htaccess to skip the requests made for the blog,
rewriteCond %{REQUEST_URI} !^/blog.*
Here it is how it looks. I removed the rest of the file:
Options +FollowSymlinks -MultiViews RewriteEngine on
RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
// my added line RewriteCond %{REQUEST_URI} !^/blog.*
RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(all)/([^/]+)/?$ story.php?title=$2 [L] RewriteRule ^(all)/?$ ?category=$1 [L]
RewriteRule ^/?$ index.php [L] RewriteRule ^advanced-search/?$ advancedsearch.php [L] ...
The problem I have is related to the blog requests. For example sometimes if I try to open an url it works fine, sometimes the home (root page not the blog) is opened. It seems very strange. I think it is related to the host. When the host is to busy the blog page I request is not found so the request is going to the root .htaccess.
I have 2 questions:
how to write a rule and where to
place it to exclude all the requests
for /blog to be rewritten by the root
.htaccess? the blog requests might
look like http: //test.com/blog,
http: //test.com/blog/,
http: //test.com/blog/title,
http: //test.com/blog/title/,
http: //test.com/blog/category/title
does anyone has any idea what happens? Why when I open a blog page it opens the home root page, and if I refresh the page it goes to the blog post page?
Take a look into the mod_rewrite documentation, specifically the -d flag in RewriteCond.
It should be something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule HERE

Categories