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.
Related
Here's my full case:
mvcframework directory inside /var/www/html/ dir.
mvcframework contains a public dir with index.php acting as a frontloader.
I am creating a .htaccess file inside public directory with following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
</IfModule>
I am accessing this through virtual host url mvcframe.local that is setup to direct at public folder:
<VirtualHost 127.0.0.3:80>
ServerName mvcframe.local
DocumentRoot /var/www/html/mvcframework/public
<Directory /var/www/html/mvcframework>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/www/html/mvcframework/mvc-error.log
CustomLog /var/www/html/mvcframework/mvc-access.log combined
</VirtualHost>
When I access http://mvcframe.local or http://mvcframe.local/ it outputs index.php content inside public folder index.php as it should.
When I access http://mvcframe.local/?posts/hello it outputs:
Requested URL = posts/hello
But when I access http://mvcframe.local/posts/hello removing ?, it gives:
Not Found
The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at mvcframe.local Port 80
I am trying to figure out the solution searching for 2 hours and still haven't got the solution for it.
Alright, so after messing around and reading .htaccess tutorials for hours, I finally managed to understand my mistake. First of all, here is the video which helped me understand my mistake.
Here's my solution:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
</IfModule>
First, I am checking if mod_rewrite is enabled. Then providing some options. Then turning on the RewriteEngine which is neccessary for enabling rewriting rules. Then giving the Rewrite Conditions for [If no directory] and [If not file] for whatever query is being supplied, for which I rewrite the rule basically telling it that check for all string that doesn't end with extension ".", simply direct them all to index.php?$1.
Basically anything that has mvcframe.local/something_here/some_here/... will become automatically mvcframe.local/index.php?something_here/some_here
For my setup, index.php is the frontloader so everything must go through this file.
I'm trying to adapt the .htaccess file in the root of my CodeIgniter installation so I don't have to include index.php in all the URLs leading to my controllers.
This is the content of my .htaccess file:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
DirectoryIndex login.php
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteCond $(REQUEST_FILENAME) !-f
RewriteCond $(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1
As you can see, it's the default .htaccess file with some rewrite rules for mod_rewrite and a DirectoryIndex directive appended to it. Mod_rewrite is enabled (I've checked this using apachectl -M).
Now, I'm running Apache 2.4.18 on Ubuntu 16.04 LTS and I am using CodeIgniter 3.1.6. The problem is that when I type in no URL I get redirected to CodeIgniters index.php instead of my wanted login.php. I'd rather get the DirectoryIndex directive working instead of transferring the contents of login.php into index.php for obvious reasons. My mod_rewrite rules are meant to route URLs to existing files "through" index.php without having to include index.php in their URL. This way, my URLs are cleaner but I can still use CodeIgniter's facilities.
My master Apache configuration obviously allows me to use .htaccess files (since CodeIgniter needs this too) and the URL rewrite directives work just fine. Why does the DirectoryIndex not seem to do anything no matter where I place it inside the file?
Check out if you have AllowOverride instruction set to All -- it controls if .htaccess file has any power.
There are the answers on how to set it: How to Set AllowOverride all
I fixed the problem but not in an elegant way.
It appears to me that mod_rewrite just doesn't work (correctly) when you use it in a .htaccess file of a subdirectory of your DocumentRoot.
My CodeIgniter installation was in a subdirectory called "CodeIgniter" inside the DocumentRoot. I made two .htaccess files:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %1 !^index.php
RewriteRule ^(.*)$ /CodeIgniter/index.php [L]
DocumentRoot/CodeIgniter/.htaccess:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
Options FollowSymLinks
DirectoryIndex login.php
This seems to be doing the trick for me
Moral of the story: do all of your URL rewriting using mod_rewrite in the .htaccess located directly in your DocumentRoot
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.
I'm trying to make my links a little easier to write, without the .php.
My rewrite rule is:
RewriteEngine on
RewriteRule ([a-z]+)$ $1.php
This is the first .htaccess I wrote myself, but it doesn't seem to work.
I'm good with RegExp. And I don't see the issue here.
Thanks for the help.
Make sure mod_rewrite is enabled in apache config as well as AllowOverride All
LoadModule rewrite_module modules/mod_rewrite.so
<Directory "c:/wamp/www/">
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</Directory>
To get it workign in wamp
Click on wamp icon in systray
Go to apache
then click on httpd.conf
Search the file for mod_rewrite.so. If that line is commented (#) uncomment it.
It should look like
LoadModule rewrite_module modules/mod_rewrite.so
Then look for something like
<Directory "c:/wamp/www/">
You should find a AllowOverride none Change it to AllowOverride all
Restart your wamp server
Then set your .htaccess to nickb suggestion
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If it still doesn't work just do a search in the httpd.conf for AllowOverride none and change them all to AllowOverride all, as there may be a couple
RewriteEngine on
RewriteRule ^(.*)$ $1.php
Start from here: http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
Even though yours works for alphabetic filenames, this is what I've used regardless of the name / path of the file to omit the .php extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It makes sure you're not trying to request a directory, and that the file that was requested is a PHP file.
I want to have a user friendly url such as "http://localhost/Symfony/web/app_dev/" instead of "http://localhost/Symfony/web/app_dev.php/" . I have tried editing the htaccess but still does not work. My htaccess in the web dir is :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I suppose it should have worked.. How can I make this work and what is the problem in the htaccess codes if any?
Try to add into your vhost configuration following lines:
<Directory "/path/to/docroot">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Try adding RewriteRule ^app_dev(.*)$ app_dev.php [QSA,L] before RewriteRule ^(.*)$ app.php [QSA,L].
You must enable the RewriteEngine in the apache configuration:
sudo a2enmodule rewrite
Regards,
Max