I just moved a development site from a virtual machine on my PC to a shared webhost in a subdomain and I'm getting a Forbidden error which I think is due to my .htaccess file. Currently I've got it set up like so:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Remove www prefix
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>
I tried commenting out the RewriteBase line and the "remove WWW prefix" bit which doesn't seem to help. Currently my site resides at mysite.mysharedhost.com and I still need to be able to remove the PHP extensions from my files. Am I missing something important or do you think my error is unrelated to my .htaccess file? Thanks in advance!
Related
When users go to my sub domain, I want them to be redirected to a specific file I have in my subdomain. Or, to be exact, when someone goes here
http://app.example.com/
they should be redirected to here:
http://app.example.com/dashboard/
Everything I have tried doesn't seem to be working and the examples I have found online either don't work or don't do what I need them to do.
I'm also trying to get this to work from my root directory (from public_html). This is my file structue:
/ public_html
/ .htaccess
/ index.php
/ app.example.com
/ dashboard.php
You can see my htaccess file sitting on the top level. This is where I need to keep it.
This is my current file (including my attempt at the redirect):
# --------------------------------------------------------------------------------------------------------------------------------------
# Stops people from being able to see htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# --------------------------------------------------------------------------------------------------------------------------------------
# Options
Options All -Indexes +MultiViews
# --------------------------------------------------------------------------------------------------------------------------------------
# Redirect app.example.com to /dashboard
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.example.com
RewriteRule ^$ /dashboard [L,R=301]
# --------------------------------------------------------------------------------------------------------------------------------------
# Makes it so url can have slash at the end instead of .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# --------------------------------------------------------------------------------------------------------------------------------------
# Adds wwww to url if it doesnt already have it
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
I have just launched a Laravel 5.3 project on a live server and all is going well except one.
But my issue is, My websites are running on all 3 different URL.
My server is Ubuntu 16.4.
website.com
website.com/public/index.php
website.com/ any word /index.php
My .htaccess is: (This file is in the root folder)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>
But I just want to see my website with website.com URL only.
Seems like your Laravel app is accesible via an Apache HTTP alias. Follow these steps
Try to navigate to your expected route prepend it with /index.php/, in your case: website.com/index.php/users. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps.
Edit the file public/.htaccess.
Under the line RewriteEngine On add RewriteBase / if files are on root directory if in folder like laravel then add RewriteBase /laravel/
Try to navigate to an existing route.
i.e
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>
Remove /public from your Laravel
create a virtual host
go to /etc/apache2/sites-available create .conf file or update 000-default.conf
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/html/index.html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =website.com [OR]
RewriteCond %{SERVER_NAME} =www.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
To only match the homepage, i.e. http://domainxyz.com/ you need to match the empty path (as mod_Rewrite removes the leading /.
RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC]
RewriteRule ^$ /view.php?page=process/ [NC,L]
Try using this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com/([a-zA-Z-]*)/index.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com/([a-zA-Z-]*)/index.php [NC]
RewriteRule ^(.*)$ http://www.website.com [L,R=302,NC]
Make sure you clear your cache before testing this. You will notice I've used R=302, this is a temporary redirect. If the redirect works and you're happy with it, then switch that to R=301 which is a permanent redirect.
This code worked for me. It will remove extention from your website e.g .php and all. Try it once.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
1. I understand why you are trying to change the .htaccess but this can be solved alot simpler. Revert the .htaccess file back to the Laravel standard
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
2. Move all of your folders and files apart from public and make sure they are behind your HTML folder.
3. Move all of your public folders and files out of the public folder and into your html folder
4. Tell laravel to look in your html folder instead of public by altering the server.php file
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/html'.$uri)) {
return false;
}
require_once __DIR__.'/html/index.php';
I find this solution a lot simpler than messing around with you htaccess. You also ensure all of the Laravel core files cannot be accessed from a url.
Other issue you are having (Internal pages)
This is probably due to your apache not allowing override with Laravels htaccess
Go to the command line of your server and access the apache2.conf file
cd /etc/apache2
Open the apache2.conf file with nano
sudo nano apache2.conf
Now locate the following
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change this to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Writeout the changes and exit nano.
Restart apache
sudo service apache2 restart
It should now be working :)
In the past, I have got this working no problems at all, but for some reason on my new server I just can't get it to work.
I have the following .htaccess file in the root of my application
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have also enabled mod_rewrite and have confirmed this with php_info()
my site is located at /var/www/html/test
Is it possible that although mod_rewrite is enabled that it is not working and if so, how can I test it?
On some server implementations, you'll need to wrap it within <IfModule> tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Also check your httpd.conf file to ensure it has:
Options FollowSymLinks
AllowOverride All
It'd also be worth checking to makesure the .htaccess file isn't being overridden by another .htaccess file.
A simple way to test if .htaccess is working
Simply put the following in your .htaccess file:
deny from All
What this does is deny access to your site from everyone. If you are presented with a 403 forbidden when trying to access the site - the .htaccess file is being included. If not - see above.
I use this on my codeigniter setup
RewriteEngine on
# prevent these directories from hitting CI
RewriteCond $1 !^(index\.php|images|assets|robots\.txt|crossdomain\.xml)
# route everything else to the index.php
RewriteRule ^/(.*)$ /index.php/$1 [QSA]
(my setup is done in the virtualhost of .conf and not in .htaccess, though they are usually about the same configuration)
I am building a wiki using dokuwiki, and am using the nice URL feature which uses url rewriting through Apache on the webserver, to get rid of the ugly php urls. My problem is that I seem to be only getting 404 Not Found errors when searching for the pages using the new URLs.
This is the .htaccess file provided, and the RewriteBase is pointed to the correct location.
#Options -Indexes -MultiViews +FollowSymLinks
## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
Order allow,deny
Deny from all
</Files>
## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
RewriteEngine on
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
## Not all installations will require the following line. If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
RewriteBase /dokuwiki
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Make sure your Apache actually loads .htaccess files. You will probably need this in your Apache config:
AllowOverride All
So, I moved the website.com to subdomain.website.com. The .htaccess file is located in the website.com/subdomain/ folder, it should work but it's not working.
Error message: The requested URL /title-123 was not found on this server.
Basically what it does is translates the website.com/product.php?id=123&title=hello to website.com/hello-123
The error is produced when trying to access subdomain.website.com/title-123
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/product\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ product.php?id=$2&title=$1 [L]
</IfModule>
Anyone knows how to fix this? Thanks!
After a lot of research and debugging of apache conf files and receiving expert's assistance i managed to fix it removing completely the .htaccess file and adding the rewrite rules in the apache VirtualHost directly.