Let's suppose I have a website named
foo.com
I can access foo.com and it runs the index.php found in the root folder. My question is: how should I edit the vhost file to enable rewrite mod without enabling htaccess?
My goal is to be able to write
http://foo.com/bar/loremipsum/dolor
into my browser address bar and to run index.php regardless of the number of / characters in the url. My index.php would handle the parameters separated by /
How can I achieve this?
EDIT:
vhost file:
<VirtualHost *:80>
ServerName myproject.com
ServerAlias www.myproject.com
ServerAdmin webmaster#localhost
DocumentRoot /opt/apps/myproject
<Directory /opt/apps/myproject>
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EDIT: The problem, as the accepted answer suggests is that this host did not contain the line which turns the rewrite engine on. This line is present in the answer. Which solves the problem.
To disallow the use of htaccess, you need this directive in a <Directory> container for your document root, then you can just place mod_rewrite rules in the same container:
<Directory "/var/www/htdocs/">
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</Directory>
assuming that "/var/www/htdocs" is you document root.
To ensure mod_rewrite is loaded, check the httpd.conf file for a LoadModule line that contains mod_rewrite, and make sure it's uncommented. You'll need to restart your server anytime you make changes to the vhost config.
Short explanation:
The lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
are conditions to check that the request is not an existing file (-f) or an existing directory (-d). These conditions serve 2 main purposes:
It prevents the rewrite engine from looping, so that index.php won't also get rewritten. Since index.php is an existing file, the conditions stop the rewrite engine.
It allows resources and assets like images or scripts from being rewritten.
If you want everything routed to index.php no matter what (including images or anything else), then you can change the 2 conditions to simply:
RewriteCond %{REQUEST_URI} !^/index.php
so that everything gets rewritten except index.php.
Related
Currently trying to set up a Laravel project on my local PC.I added the hostname 127.0.0.1 localhost-smt to the Windows hostfile and edited my Apache httpd-vhost.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/project1"
<Directory "C:/xampp/htdocs/project1/">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost-smt
DocumentRoot "C:/xampp/htdocs/project1/smt/public"
ErrorLog "logs/testseite-error.log"
CustomLog "logs/testseite-access.log" common
<Directory "C:/xampp/htdocs/project1/smt/">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
My Apache httpd.conf
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Each time I connect to localhost-smt/home I am receiving a double URL like http://localhost-smt/127.0.0.1/home. Further more if I navigate from localhost to the public folder and add /home i receive the following URL http://localhost/smt/public/127.0.0.1/smt/public/home.
I tried a lot of different solutions, but none of them worked so far. I would appreciate if someone could help me solve this riddle.
I'm not sure how to do this using .conf files but the way I used to achieve this when I used Xampp was as follows, doing it this way will obviously mean undoing anything you have done to the default behaviour of Xampp relating to the .conf files you have edited.
Add the name resolution to your hosts file
127.0.0.1 dev.projectName
Add the following .htaccess file into the root of your projects directory (typically "C:/xampp/htdocs")
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev.projectName$
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ projectName/$1 [L]
add the following .htaccess file to the root of your project (typically "C:/xampp/htdocs/projectName")
RewriteEngine On
RewriteBase /dev.projectName/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Add the following .htaccess file into your public directory (typically "C:/xampp/htdocs/projectName/public")
RewriteEngine On
RewriteBase /dev.projectName/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
this can be viewed by going to http:\\dev.projectName
I know it is a long-winded way of doing it but it was the only solution I could find at the time and it worked for me.
It does become a ball-ache having to do this every time you create a new project which is one of the reasons I switched to docker which in my opinion is a much better solution anyway,
LARAVEL 5.0 | APACHE 2.4.7 | PHP 5.5.8
I know this is a pretty popular question. A lot of people saying to move public contents to root directory (which i find awkward because of security issues), rewriting the .htaccess, creating virtual host.
I tried creating virtual host because i think this is more acceptable solution.
but i'm encountering some problems with my configurations:
I'm running easyphp for the meantime, id like to test if i can get rid of the public route in url before setting this up live.
httpd.conf
NameVirtualhost *:80
<VirtualHost *:80>
DocumentRoot "${path}/data/localweb/quantum.dev/public"
ServerAdmin admin#localhost
ServerName quantum.dev
ServerAlias www.quantum.dev
<Directory "${path}/data/localweb/quantum.dev/public">
AllowOverride All
Options Indexes FollowSymLinks
Order deny,allow
Allow from quantum.dev
Deny from all
Require all granted
</Directory>
</VirtualHost>
I modified C:\Windows\System32\drivers\etc
hosts
127.0.0.1 quantum.dev
When I try to browse http://127.0.0.1/quantum.dev/ it will load the listof files and not the project inside public
http://127.0.0.1/quantum.dev/
Please revert all your changes to the directory. restore the default structure and configuration of the project. If i'm not mistaken and correct me if i am, you are using a shared hosting provider. If that is the case, here is a more proper way of doing so.
In your .htaccess, you need to add the following.
At the top of the file add this line:
DirectoryIndex public/index.php
and in here:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Change the last rule to this:
RewriteRule ^ public/index.php [L]
Now, all you need to do is to move this, and only this(the .htaccess file), to the root folder of your project. In this way, you are maintaining the laravel project structure in its original state.
put this .htaccess file near public folder
Options -MultiViews
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]
When you are selecting a directory for your domain in c-panel, simply provide path till the Laravel's public directory and not just till the Laravel's root directory. That way you don't need to change .htaccess at all.
I have just recently upload my site to the production and follow above steps. It worked very well.
This is my configuration that is working fine for me. So first of all, your Virtual host configuration should be like that :
ServerAdmin webmaster#domaine.dev
ServerName domaine.dev
DocumentRoot /var/www/mysiteweb/public/
<Directory /var/www/mysiteweb/>
Options -Indexes
DirectoryIndex index.php index.html
AllowOverride All
</Directory>
Anf after, make this on your .htaccess laravel file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Don't forget to check if the mod_rewrite is enable.
Hope that helped. Regards.
i am very begainer to server technologies.i know this question asked too many times but i really don't get it run.
it runs with domain.com/2.0/index.php/testbut when you remove index.php it throws
The requested URL /2.0/test was not found on this server.
Apache/2.2.15 (CentOS) Server at domain.com port 443
my project stucture
-
Public_html
--2.0(this is my project folder where ci can be located.it has sys,app,.htaccess)
--.htaccess
My domain
https://domain.com/2.0/
about the problem
i have two htaccess one at root mention above one is inside 2.0 folder
and the strange thing is what ever i write in the htaccess file it never effect anything
public_html/.htaccess
<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>
public_html/2.0/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
my httpd.conf
<VirtualHost *:443>
ServerAdmin info#domain.com
DocumentRoot /home/natural/public_html
ServerName api01.domain.net
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/access_log common
SSLEngine on
SSLCertificateFile /home/natural/api01.domain.net.crt
SSLCertificateKeyFile /home/natural/api01.domoin.key
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride None
</Directory>
</VirtualHost>
i search alot and follow all the answers by Stack overflow but no luck.
The directive "AllowOverride None" in httpd.conf will disable parsing of the .htaccess file within the directories specified (in this case /) -- Can you confirm that either of your .htaccess files are in fact being parsed?
If not, does modifying the Directory subsection within your httpd.conf file to:
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride All
</Directory>
Solve the issue?
For more information: http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I assume 2.0 is the new version of the site. It looks like /2.0/.htaccess is written as though it expects to be in the root folder. Try adding RewriteBase /2.0/ after engine on. You can remove it later when you replace the contents of the root folder with that of 2.0's.
The main problem is your # Handle Front Controller... rule. It'll capture any virtual URLs before they ever get to the 2.0 folder. Try changing it to this:
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]
That will exclude the 2.0 folder from the current site's virtual URLs.
You do still need AllowOverride all as recommended by the other answer, and the directory should be the same as your DocumentRoot, not /.
Add this code inside root->.htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]
I have set up an Apache server with the following URL: https://dev.mysite.com. I am now creating subdirectories for each project that I have in development. For example it might be https://dev.mysite.com/project, or https://dev.mysite.com/anotherproject. Some of the projects in development utilize the Laravel, and the Laravel Lumen, framework(s).
I will not be using a vhost file for each project, as they will be existing in subdirectories. The goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, or Lumen, application served to the user visiting the project. I am ok with having the client visit /public/ to view their project. This is not happening.
-Indexes has been set on the server's virtualhost file to prevent listing files/directories in the browser.
When I navigate to {url}/project/public/heartbeat (for example) the route is not being served (or parsed) correctly. When I do a die of the $_SERVER['REQUEST_URI'] I get /project/public/heartbeat. The route heartbeat is set up as follows: $app->get('/heartbeat', 'Controller#method');. Visiting this url gives the typical HTTP not found exception from the Illuminate code. However; when I add project/public/ to the beginning of the route path (resulting in $app->get('/project/public/heartbeat'...) it works. I have tried adding the RewriteBase / and RewriteBase /project/public/ and RewriteBase /project/ settings to the .htaccess file and neither work. I do know that the .htaccess is being used (I can add 'asdf' to the file and break it). The mod_rewrite module is enabled.
Here are the configuration files for the vhost, and the project/public/.htaccess file.
.htaccess:
<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>
vhost:
<VirtualHost *:80>
ServerName dev.mysite.com
Redirect permanent / https://dev.mysite.com
</VirtualHost>
<VirtualHost *:443>
ServerName dev.mysite.com
DocumentRoot /var/www/public
<Directory /var/www/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/dev-error.log
CustomLog ${APACHE_LOG_DIR}/dev-access.log combined
ErrorDocument 403 https://dev.mysite.com
</VirtualHost>
Please let me know if you need any further information. Again, end goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, Lumen, etc. application served to the user visiting the project.
Setup your vhost as follows:
<VirtualHost *:80>
DocumentRoot "/some/directory/mysite.dev"
ServerName mysite.dev
</VirtualHost>
Notice you need to setup an initial 'root' project at /some/directory/mysite.dev. This doesn't have to be a laravel app, all it needs to have is a .htaccess file, and maybe you might want an index.html file in there so when you visit mysite.dev, you can at least see something. This is optional though.
In the /some/directory/mysite.dev folder, create a .htaccess file and add the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# We need this to stop the rewrite rule looping as the URI
# and directory both start with 'SubProject'
RewriteCond %{REQUEST_URI} !^/SubProject/
# direct all requests that start with SubProject
RewriteRule ^ SubProject/?(.*)/?$ /SubProject/public/index.php [L]
</IfModule>
When you visit mysite.dev/SubProject the index.php file from that project should be loaded.
This works:
RewriteRule ^newest/?$ index.php?type=newest
This does not work:
RewriteRule ^newest/(\d+)*/?$ ./index.php?type=newest&p=$1
The rest of my re-write:
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
This works on my localhost running xampp, but it not working on my web host. What could be the problem before I contact them?
A couple of things to check ...
You might need to enable htaccess in your apache config file (httpd.conf) by uncommenting the following:
;LoadModule rewrite_module modules/mod_rewrite.so
Try ensuring that the directory entry in httpd.conf for your server doesn't contain
AllowOverride None
as this will prevent the .htaccess file from being used in the individual directory. It should look something like this (note the AllowOverride All):
<Directory /var/www/www.mysite.com>
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
Also in httpd.conf, make sure that .htaccess is actually the name apache expects for access files. The AccessFileName directive can specify this value. For example:
<virtualhost>
ServerName www.mysite.com
DirectoryRoot /var/www/www.mysite.com
AccessFileName .customhtaccess
</virtualhost>
If the AccessFileName directive is set to something different, an .htaccess file won't be parsed.
What is your intent with this rule
RewriteRule ^newest/(\d+)*/?$ ./index.php?type=newest&p=$1
If it is to match 0 or more digits it should be
RewriteRule ^newest/(\d)*/?$ index.php?type=newest&p=$1 [NC,L]
If it is to match 1 or more digits it should be
RewriteRule ^newest/(\d)+/?$ index.php?type=newest&p=$1 [NC,L]
Why don't you use a more efficient way to do this. I recommend to use this pattern:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
To redirect all of requests to your index file then you're able to parse URL and do what yo need. This is not the correction for your question but it would be an alternative way to avoid your kind of working.