I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server.
Are there any good references so I can learn more myself?
RewriteEngine On
RewriteRule something something.php [L]
http://example.com/something will be handled as if it was a request for something.php
To redirect all requests that are not a physical file to the same name but with .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
The accepted answer above does not remove the .php extension from urls it just allows you to visit .php files without extension. To remove the .php extension completely from urls , you can use the following Rules in root/.htaccess :
RewriteEngine on
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
Probably what you really want is just a way to not have the suffix at all. The
best way I've found of doing this is to use the Files directive in .htaccess or in
apache configuration files:
<Files myscript>
SetHandler cgi-script
</Files>
This avoids some of the downsides of rewrites, e.g., direct access of the .php
file.
.htaccess file: add the following lines:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
Now you would have to check for the AllowOverride condition in apache2.log file:
set options AllowOverRide All in your web root directory:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file:
/var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
To fix this: You don't have the mod_rewrite module installed, To install it:
Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Again, after refreshing the webpage, you might get the folloowing error in error.log file:
[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)
To fix it you would have to add the following lines in apache2.conf:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .html
AddType application/x-httpd-php-source .phps
</IfModule>
https://forums.digitalpoint.com/threads/htaccess-addtype-application-x-httpd-php-php-htm-html-and-maybe-a-fix.7584/#post-2491687
Now, I can access the test.php file using just the filename: test
For some reason I wasn't able to get any other solution working properly. I put this in an .htaccess file at the root of my site. Hopefully this will work for you.
RewriteEngine on #only use this once per .htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It's important to keep in mind that if your header or URL contains the .php extension it won't be removed. You'll need to remove the extension.
Use this...
header 'location: /login';
rather than this...
header 'location: /login.php';
This Option will make clean SEO friendly URL's. Removing.php extensions from the visible URL, and ensuring that URL's accessed without the .php will still display the .php file. I was unable to find another answer that achieved this on multiple threads.
Remember to enable mod rewrite. On Debian or Unbuntu linux the following will work
sudo a2enmod rewrite
and
sudo service apache2 restart
Modify the corresponding section of your apache2.conf file to the following.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</Directory>
If using .htaccess to re-write ensure that the apache2.conf at least has these options enabled.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
Finally on some Apache install you may need to also set
AllowOveride All
in a .conf file in /etc/apache2/sites-available or /etc/apache2/sites-enabled.
Rewriting from apache2.conf file as opposed to .htaccess is rumored to be faster.
To get http://example.com/test linking http://example.com/test.php use:
RewriteEngine on
RewriteRule ^([^\.]+)$ /folder/$1.php [NC,L]
For directing a few single files it might be easier to avoid the RewriteEngine.
I've used this:
<Files $filename>
SetHandler application/x-httpd-php
</Files>
That's less powerful and flexible than rewrite but probably faster and seems like the way to go if you wish to just have a few specific files handled as PHP.
RewriteEngine On
# Redirect sites with no extension to .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}.php
This is what I came up with. This is based off of the accepted answer and its comments.
Related
How to access my .html or .php file without extension?
This is my Folder structure:
C:\Apache24\htdocs\search-html\test
search-html
test
low.html
high.html
new.html
My URL is 127.0.0.1/search-html/test/low.html
But i want to access this URL like 127.0.0.1/search-html/test/low
for running localhost server I use httpd.exe -k start on command prompt
Use the following in your .htaccess file. It doesn't really matter where the .htaccess file is located, providing it is inside the document root and somewhere along the file-path being requested.
If you are requesting URLs of the form http://127.0.0.1/search-html/test/low then C:\Apache24\htdocs is your DocumentRoot (looking at your "folder structure"), as defined in the server config.
RewriteEngine On
# Append ".html" extension if the file exists
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule !\.\w{2,4}$ %{REQUEST_URI}.html [L]
# Otherwise append ".php" extension if the file exists
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule !\.\w{2,4}$ %{REQUEST_URI}.php [L]
The above rules specifically exclude any URL that already includes what looks-like a file extension. So static resources (images, JS and CSS, etc.) will naturally be excluded.
Alternatively, if you are doing nothing else in .htaccess and want extensionless URLs then just enable MultiViews. For example:
Options +MultiViews
However, this does have some caveats:
Extensionless URLs are essentially enabled on everything, not just .html and .php files. Including images, JS and CSS etc.
If you later want to do more complex URL rewriting with mod_rewrite then you may need to disable MultiViews and use the mod_rewrite solution instead. MultiViews and mod_rewrite can result in unexpected conflicts.
You can add a .htaccess file to the document root to rewrite the url.
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
And if you want to add a trailing slash at the end you can add this bit.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
I want a mod_rewrite rule set, so I can refer to a page without the .php extension, but have that rewritten to include the .php extension. This will be running on a 1&1 server.
Are there any good references so I can learn more myself?
RewriteEngine On
RewriteRule something something.php [L]
http://example.com/something will be handled as if it was a request for something.php
To redirect all requests that are not a physical file to the same name but with .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
The accepted answer above does not remove the .php extension from urls it just allows you to visit .php files without extension. To remove the .php extension completely from urls , you can use the following Rules in root/.htaccess :
RewriteEngine on
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
Probably what you really want is just a way to not have the suffix at all. The
best way I've found of doing this is to use the Files directive in .htaccess or in
apache configuration files:
<Files myscript>
SetHandler cgi-script
</Files>
This avoids some of the downsides of rewrites, e.g., direct access of the .php
file.
.htaccess file: add the following lines:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
Now you would have to check for the AllowOverride condition in apache2.log file:
set options AllowOverRide All in your web root directory:
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file:
/var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
To fix this: You don't have the mod_rewrite module installed, To install it:
Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Again, after refreshing the webpage, you might get the folloowing error in error.log file:
[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)
To fix it you would have to add the following lines in apache2.conf:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .html
AddType application/x-httpd-php-source .phps
</IfModule>
https://forums.digitalpoint.com/threads/htaccess-addtype-application-x-httpd-php-php-htm-html-and-maybe-a-fix.7584/#post-2491687
Now, I can access the test.php file using just the filename: test
For some reason I wasn't able to get any other solution working properly. I put this in an .htaccess file at the root of my site. Hopefully this will work for you.
RewriteEngine on #only use this once per .htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It's important to keep in mind that if your header or URL contains the .php extension it won't be removed. You'll need to remove the extension.
Use this...
header 'location: /login';
rather than this...
header 'location: /login.php';
This Option will make clean SEO friendly URL's. Removing.php extensions from the visible URL, and ensuring that URL's accessed without the .php will still display the .php file. I was unable to find another answer that achieved this on multiple threads.
Remember to enable mod rewrite. On Debian or Unbuntu linux the following will work
sudo a2enmod rewrite
and
sudo service apache2 restart
Modify the corresponding section of your apache2.conf file to the following.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</Directory>
If using .htaccess to re-write ensure that the apache2.conf at least has these options enabled.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
Finally on some Apache install you may need to also set
AllowOveride All
in a .conf file in /etc/apache2/sites-available or /etc/apache2/sites-enabled.
Rewriting from apache2.conf file as opposed to .htaccess is rumored to be faster.
To get http://example.com/test linking http://example.com/test.php use:
RewriteEngine on
RewriteRule ^([^\.]+)$ /folder/$1.php [NC,L]
For directing a few single files it might be easier to avoid the RewriteEngine.
I've used this:
<Files $filename>
SetHandler application/x-httpd-php
</Files>
That's less powerful and flexible than rewrite but probably faster and seems like the way to go if you wish to just have a few specific files handled as PHP.
RewriteEngine On
# Redirect sites with no extension to .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}.php
This is what I came up with. This is based off of the accepted answer and its comments.
I am currently using an htaccess file to remove the php extension from my website files. It is working properly on one of my websites, but not on another and we're using the same code. (but with additional redirects on the one that is broken)
The weird part is - it has been working for months and only stopped working when Media Temple updated our grid server from Debian Squeeze to Wheezy (and they claim that shouldn't have changed how the htaccess works, but not sure what other explanation there is.)
Apparently it does still redirect html files. Any ideas why?
Current code:
# BEGIN (mt) controlled settings
<IfModule !mod_fcgid.c>
AddHandler php-stable .php
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files *.php>
Options +ExecCGI
</Files>
</IfModule>
# END (mt) controlled settings
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
ErrorDocument 403 /error-403
ErrorDocument 404 /error-404
ErrorDocument 500 /error-500
That code is followed by our 301 Redirects.
The host finally figured out the issue after saying it wasn't the update to Debian's fault. This is what they said:
the recent Wheezy update to the Grid had indeed modified a few >unexpected things, including the way MIMEs are configured which is why >+MultiViews was working for [site 1], which contains only .html files, >while [site 2] was not working, which contains .php files. To correct this, I have added the following line to the ~/domains/[site 2]/.htaccess file so that it interprets .php files correctly:
AddType application/x-httpd-php .php
So the code now looks like this:
AddType application/x-httpd-php .php
# BEGIN (mt) controlled settings
<IfModule !mod_fcgid.c>
AddHandler php-stable .php
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files *.php>
Options +ExecCGI
</Files>
</IfModule>
# END (mt) controlled settings
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
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 know this question is more appropriate for Server Fault but unfortunately I was banned for poor quality questions (I was down voted on 2-3 questions I asked.) So the next best place to ask these questions are here.
I have two problems related to CodeIgniter routing.
The first problem is that I can't seem to get rid of index.php in the url. I followed the instructions on how to remove it. I have the following mod rewrite code in my .htaccess file (see below) at the root of my WAMP server (CI is located at the root, not in its own folder). I have uncommented this line in httpd.conf file LoadModule rewrite_module modules/mod_rewrite.so. I deleted index.php from $config['index_page'] = "index.php";. And I restarted all WAMP services.
My second problem is that I have a controller called search and a method called index. I would like to change the resultant URL from http://localhost/index.php/search/index to http://localhost/search/whatever_im_searching_for. I tried the following custom route in my routes.php file but it did not work: $route['search/(.*)'] = "search/$1";
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
I am struggling to understand the code in .htaccess and on how to use CI's custom routing. Any assistance will be greatly appreciated. Thank you in advance.
EDIT 1
Edit your htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|images|table-images|js|robots\.txt|css|captcha)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
To have your searchterms in the url you can look at this:
https://stackoverflow.com/a/12070284/1379394
Second problem:
$route['search/(:any)'] = "search/index/$1";
Check Apache's default config file. On WAMP it's probably in
<WAMPSERVER_HOME>\bin\apache\Apache2.2.xx\conf
If it looks like this:
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
then change both:
AllowOverride None
to:
AllowOverride All
I had the same problem and I fixed only by write in my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
and it's working perfectly.