Rewrite every url to go to directory - php

I have a folder structure like this
/first/second/{application}
And I would like that trough htaccess to load content from application without those two directories to appear. So www.domain.com would load www.domain.com/first/second and www.domain.com/something/anything would load www.domain.com/first/second/something/anything
I tried with RewriteRule ^(.*)$ /first/second/index.php/$1 [L] in htacces in root folder but without any success.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/first/second/
RewriteRule ^(.*)$ /first/second/$1

Related

Remove Subfolder[Codeigniter] only for specified URLs using Htaccess

My Folder Structure
Root Domain
assets/ (contains css, images, js folders)
subfolder/ (contains Codeigniter Project inside and seperate .htaccess)
index.php
contact.php
.htaccess
Root Htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]
Subfolder(Codeigniter Project) Htaccess
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For godady Shared Hosting Server uncomment the line below
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
List of Subfolder URLs
http://example.com/subfolder/products/
http://example.com/subfolder/product/product-slug-123
List of Root URLs
http://example.com/index.php
http://example.com/contact.php
In my Codeigniter Project I've set Products controller as default_controller.
So the problem is, when I try to access the index.php page in the Root folder it only displays the Codeigniter products list as Home page.
I need following URLs:
http://example.com/index.php
http://example.com/contact.php
http://example.com/products/
http://example.com/product/product-slug-123
Any solutions with htaccess or with code?
Finally I got this working.
RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]
Exclamation(!) symbol used to 'not' matching urls.
So I removed Exclamation symbol and used another RewriteCond (Condition) to redirect only if the url contains segment as products or product http://example.com/products/ or http://example.com/product/product-slug-123. It's working perfectly now. I thought, sharing this would be useful to someone. Happy coding.

.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?

Directory access

I'm writing web application using codeigniter framework. Here is the structure of document root
application
system
website
What I want is that only the website directory to be accessаble. Here is the content of .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule .* /website/%1 [QSA]
When I type the site url in browser I'm redirected to website dir. So far, so good. It works.
The problem comes when I try to access a dir within the website directory. I get
404 page not found
What to do so the directories below the website dir to be accessаble ?
This is the rule you need:
RewriteRule (?!^website(/.*|)$)^.*$ website%{REQUEST_URI} [L,NC]
Try
RewriteEngine on
RewriteBase /
RewriteCond $1 !^website
RewriteRule ^(.*)$ website/$1 [L]

How to load contents from different directory for www.website.com

How to display contents from www directory for http://www.website.com
i..e when a user visits http://www.website.com i want to display the contents from /www directory but keep the url same.
I have tried few methods, but for all of them the url changes as well....
Though i want to load the content from http://www.website.com/www/ i want to keep the url as http://www.website.com/
And same for other pages., i..e http://www.website.com/products.php : /www/products.php
You can use an htaccess file
RewriteEngine on
# Only apply to website.com URLs outside the www folder, but ignore real files and folders
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
# Finally redirect the topmost folder itself
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteRule ^(/)?$ www/index.php [L]
If you are running apache, you can change the root folder in the httpd.conf file (UserDir)
http://httpd.apache.org/docs/2.0/mod/mod_userdir.html#userdir
So you would have to do something like:
UserDir /www/

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

Categories