Rewrite root directory to subdirectory - php

I am developing a website that runs a backend and a frontend out of two different directories.
/frontend/
/admin/
I would like to run the /frontend/ as the root whilst still running the /admin/ as it's own directory.
What do I need to write into the .htaccess to do this.
This is how my .htaccess file looks currently:
# Set the default handler.
DirectoryIndex index.php
# Prevent directory listing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 /admin/404.php
</IfModule>
Any and all answers will be appreciated.

Add this after RewriteEngine On:
# Redirect any requests within frontend/, removing frontend/
RewriteRule ^frontend/(.*)$ http://www.example.com/$1 [R=301,L]
# Rewrite anything that is not in admin/ to frontend/
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ frontend/$1 [L]
Be sure to update the base website URL on line 2.

Related

Integrating wordpress to symfony 3 based website

I have a website that uses Symfony3 and am trying to integrate a wordpress blog under www.website.com/blog.
So what I've done so far is create a blog folder, under the Symfony web folder, which contains the wordpress files.
I've then added a rewrite rule, in the web/ directory's .htaccess file so that when we are under blog/, Symfony's url rewriting won't do anything :
# Passing through the Url rewriting for all blog/ urls
RewriteRule ^blog/(.*)$ blog/$1 [PT]
So far it's good. I can access the admin section and everything but the problem is that when I try to access www.website.com/blog or even www.website.com/blog/index.php I only get the list of files in the folder and not the actual front page.
And here is the .htaccess file from blog/ folder that was created when I launched the wordpress installation script:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /xxx/web/blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /xxx/web/blog/index.php [L]
</IfModule>
# END WordPress
Any ideas why ?
I think that if you have access to the sites-available's configuration file for your site, i.e. /etc/apache2/sites-available/mysite.conf.
And add a <Directory> for your blog with the WordPress rules:
<Directory /var/www/mysite/web/blog>
DirectoryIndex index.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
</Directory>
Note: I have added DirectoryIndex index.php by myself because my Symfony have a DirectoryIndex app.php or DirectoryIndex disabled.
If you don't have access to your configuration file, probably you need to add the DirectoryIndex index.php to your .httaccess but then your configuration site and the .httaccess of Symfony should allow this directive, e.g., they should contain AllowOverride All or AllowOverride Indexes FileInfo.

symfony2 - remove /web from urls

I'm managing a website written in symfony 2.2, hosted on a web hosting service.
I'm pretty much a complete neophite in web development, so I'm probably making some stupid mistake.
Anyway, I want to be able to access the site without the /web part.
Example: 'domain.com/symfony_site/web/mypage' -> 'domain.com/symfony_site/mypage'.
Let me say from the start: I cannot manually configure apache on the server, I only have FTP access to it.
I think I'm left with .htaccess files solution.
I have tried each and everyone of the solutions I found on the internet (most of them from this site) and none worked.
And yes, mod_rewrite is enabled.
I feel like the simplest solution would be to add a .htaccess file in the root directory of the site (the one which also contains the /web folder), with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/$1 [QSA,L]
</IfModule>
And this does seem to redirect properly, but then I get a "The requested URL web/mypage was not found on this server." if I visit 'domain.com/symfony_site/mypage'.
The desired home page 'domain.com/symfony_site' intead gives a "You don't have permission to access /symfony_site/ on this server.", so it looks like this is not even redirecting properly.
First, you need to move the .htaccess from the web directory to the root directory (the DocumentRoot).
Then, open the moved .htaccess, search all occurrences of /app and replace them by /web/app.
The .htaccess (without comments) should looks like :
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/web/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /web/app.php/
</IfModule>
</IfModule>
Now your application can be correctly browsed using the project root directory as DocumentRoot.
This change may involve to adapt the way of loading your assets.
NOTE: It's a quick-and-dirty alternative assuming you cannot change the DocumentRoot of your application to make it points to the web directory.
a symfony2 app is designed to have the document-root at web/
here is an example configuration for apache vhost.
<VirtualHost *:80>
ServerName your.domain
DocumentRoot "/path/symfony/web"
<Directory "/path/symfony/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
so your rewrite rule is not necessary

.htaccess affects directory in the sub domains

Consider this is my domain www.example.com
I am using laravel in my website
This is the laravel structure
app/
bootstrap/
public/
vendor/
server.php
To remove the index.php and public from the url i followed this answer
i.e.,
had the .htaccess in the root path
and renamed the server.php file as index.php
Here is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
It works good, but the problem is i am having subdomain as
projects.example.com
While i create a sub directory here i.e.,
projects.example.com/firstproject
It always shows the internal server error
How can i fix this like, having exception to that directory or something like that ?
The htaccess file affects the directory the file is in and all subdirectories. If you have subdirectories that you don't want mod_rewrite rules to affect, then you need to add an htaccess file with in the subdirectory with the rewrite engine turned on (so that none of the parent rules have precedence).
Just add this to an htaccess file in your subdirectory without any actual rules:
RewriteEngine On

Use htaccess to redirect root to subfolder without showing it in URL

I am currently hosting a website using the Silex framework on a shared server and I have a problem...
Silex is like Symfony, there is an app.php file located in a /web/ subfolder : the website is then only accessible via the URL website.com/web/. I cannot create a virtual host as it is a shared server, so I think the solution is to use an .htaccess file...
I managed to redirect website.com to website.com/web/ automatically but I don't really like this option. I would rather website.com pointed directly to website.com/web/ but I don't know how to do this by just using a .htaccess file. I have been trying to solve this problem for hours now and it's killing me...
At the moment I use this file :
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/app.php [QSA,L]
</IfModule>
but it just redirects you from website.com to website.com/web
Is there anyway I can make the root url directly point to the /web folder with a .htaccess file?
Thank you very much :)
If you want access to http://example.com/subdirectory just by typing http://example.com this should work.
# .htaccess main domain to subdirectory redirect
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/ [L]
Try this htaccess configuration:
DirectoryIndex web/app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule (.*) /web/$1
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /web/app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>

How to redirect website to particular file?

There is a domain name. Let's say domain.com.
When the user types domain.com only, he should be redirected to the address domain.com/file.html. How can I do that with a .htaccess file?
Plus, what does RewriteRule ^index\.php$ - [L] mean? Would it help me?
Add to your .htaccess file
DirectoryIndex file.html
Check out this site: .htaccess tricks and tips It's a good reference for rewrite rules.
As for what does RewriteRule ^index.php$ - [L] mean. It's going to ignore anything with an index.php ending Rewrite Rule meaning
To redirect you can make a index page (the first page that the user visits)
DirectoryIndex file.html
modifying your .htaccess file according to this link.
For RewriteRule you need to able the mod_rewrite module in apache and then make in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) file.html [L]
</IfModule>
In the RewriteRule you can put regular expressions to identify the url and redirect the user to a file that you want (in this case you would redirect all your links to file.html). More info here.
Moreover in the configuration file of the apache server by default is possible you have the next configuration:
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml
</IfModule>
So, by default if you have a file called index.php in your webroot of your domain.com always that file would be called first.
Try putting the following in your .htaccess file located in the root of domain.com
RewriteEngine On
RewriteBase /
#put all the following rules before any other rules in the .htaccess file
RewriteRule ^file\.html$ - [L]
#domain.com or any subdomain
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
#the root of domain
RewriteCond %{REQUEST_URI} ^/?$ [NC]
#return file.html
RewriteRule . file.html [L]

Categories