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.
Related
How would I configure my .htaccess file if I want all requests to always load a core template selection file. I believe this could be good for a few reasons the first of which being security. Only if my php template selection code flags a file as safe for display will it display. Also, it allows me to build some interesting cms functionality like requiring a php metadata array in order to load the template file. How could I accomplish this?
Also, do you think it is practical to choose this method over others?
You can use this rule in your root .htaccess to make core/template.php your front controller:
RewriteEngine on
RewriteBase /
RewriteRule ^core/template\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core/template.php [L]
EDIT: If you want even real files to be routed to same controller use last rule as:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(jpe?g|ico|gif|bmp|png|tiff|css|js)$ core/template.php [L,NC]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /index.php
</IfModule>
Make sure that inside the httpd.conf this line isn't commented:
LoadModule rewrite_module modules/mod_rewrite.so
(It should be without the '#' at the beginning)
And this is my directory tag (I'm using wamp but it should be the same, and of course restart the apache server if you changed something there)
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
I tried only remove the .php extension by using the .htaccess file on my XAMPP localhost server. I put the following lines into it:
RewriteEngine On
RewriteOptions inherit
Options +FollowSymlinks
Options -Multiviews
## hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have tried more options to accomplish my goal, but nothing changed. Did I do something wrong?
Check if you have AllowOverride All in the httpd.conf file (the configuration file for apache). If you are using XAMPP on Windows, then the httpd.conf file should reside in a directory like :
C:\xampp\apache\conf\httpd.conf
Add the AllowOverride All line in the Directory tag. For example:
<Directory C:\xampp\htdocs>
AllowOverride All
Allow from all
</Directory>
P.S. By allowing override you tell the server to allow the .htaccess file to override the default configuration of the server.
Edit: Sorry that I previously mentioned AllowOverride 'On'. It should be AllowOverride 'All'. This works fine in my ubuntu. I'll try to find out how to make it work in windows(xampp) soon.
The following works for me on apache2. Hopefully it will work on XAMPP as well.
This is nearly the same as your code, but it checks that the requested filename doesn't exist, and that the PHP file does exist.
RewriteEngine On
RewriteOptions inherit
Options +FollowSymlinks
Options -Multiviews
## hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ %{REQUEST_FILENAME}\.php [NC,L]
I am using tank auth in my project. In my localhost it is working fine. But when I uploaded the project to the server, I am getting the error as
No input file specified.
I know that it is because of htaccess. My url is like http://example.org/project/test/tankauth/. When I give this url, it will redirect to http://example.org/project/test/tankauth/index.php/auth/login with the error. However, if I try with the url like http://example.org/project/test/tankauth/index.php?/auth/login, it will work fine.
My htaccess file is as shown below:
RewriteEngine on
RewriteBase /tankauth/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I am not good in htaccess.
I assume your using apache on this one, I'm also assuming that you haven't made any changes in your apache config /etc/apache2/, and finally I'm assuming that you have an access to your server, then if that's the case, you might want to check http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
in your /etc/apache2/sites-available/default there you'll find
<Directory /path/to/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None <---change None to FileInfo or All
Order allow, deny
allow from all
</Directory>
then reset your apache -> service apache2 restart or sudo service apache2 restart
heads up using AllowOverride All may flag some serious vulnerability issues, if this is a production server I suggest you to not use it.
hope it helps, cheers!
try this (change last line)
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
and in application/config/config.php
$config['uri_protocol'] ="PATH_INFO";
It's Work.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /tankauth/
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php/$1[L]
</IfModule>
I have the following URL on my localhost:
http://localhost/?cmd=causeListByCourtName
http://localhost/?cmd=here could be any other page name
I have tried to rewrite the URL like =
http://localhost/page/causeListByCourtName
I have tried this:
RewriteEngine On
RewriteRule ^pages/(.+)/?$ .+/?cmd=$1 [NC,L]
# Handle pages requests
But it do nothing. I am using XAMPP on my windows 7.
in my httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so
is already enabled. What I am doing wrong?
You need to also make sure you changed AllowOverride None to AllowOverride All in your httpd.conf file wherever you find it.
Try doing it this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.+)/?$ /?cmd=$1 [NC,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/(.*)$ index.php?cmd=$1 [L]
This in case you use index.php as your default page. You can cut down the index.php from the front
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.