Help with URL rewriting using .htaccess - .htaccess

I have html static files on my server. I want the access rules as -
If user visits http://example.com/test/ he should get the contents of the file http://example.com/test.html
If the user visits http://example.com/test (without the trailing slash), he gets redirected to http://example.com/test/ (which runs rule 1 and gets him the contents of the file test.html)
Rules 1 and 2 should only fire if the file test.html exists.
So far, I have -
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine On
RewriteBase /
# If it's a request to index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\ [NC]
# Remove it.
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]
# Add missing trailing slashes to directories if a matching .html does not exist.
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# If it's a request from a browser, not an internal request by Apache/mod_rewrite.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# And the request has a HTML extension. Redirect to remove it.
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)/?$ $1.html [QSA,L]
</IfModule>
Although it works for http://example.com/test, it fails for http://example.com/test/ (500 internal error)
Shouldn't the second last line take care of trailing slashes?
[Update]
If I use Gumbo suggestions (.htaccess follows), I get a 404 for both http://example.com/test and http://example.com/test/ (with trailing slash)
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]
</IfModule>
[Update 2]
I have given up. The code below works, but instead of forcing everything end with a / (http://example.com/test/), it removes the trailing slash (http://example.com/test). On the bright side, it ensures that the content is pointed to by only one url, preserving SEO. I'm going to live with it for now.
Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off
ErrorDocument 404 /404.html
# Hide .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
# And there is no trailing slash, rewrite to add the .html extesion.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
</IfModule>

Try these rules:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]

Related

Codeigniter + Lumen Framework - htaccess redirect certain path to specific folder

I have a CodeIgniter project and I recently added a Laravel Lumen API, so the folder structure is the following
Project
- api/
- application/
- public/
- system/
- .htaccess
- index.php
Inside the api/ folder resides the Lumen framework. Locally it's working perfectly, by accessing the url localhost/example.com/api/test it returns 200 HTTP code.
.htaccess codeigniter root folder:
Options -Indexes
Options +FollowSymLinks
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,R,L]
RewriteCond $1 !^(index\.php|public|robots\.txt)
RewriteCond $1 !^([^\..]+\.php|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
.htaccess lumen api folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<Files .env>
Order allow,deny
Deny from all
</Files>
The errors:
By accessing example.com/api/test it returns 404 HTTP code by codeigniter
By accessing example.com/api it returns 404 HTTP code by lumen
Solved.
Well, the .htaccess worked without any problem in localhost because I have apache installed, which I don't have in the production server.
The server is running Nginx and not Apache, which I only find out after requesting support.

XCart 1.19.2.1 - enabling SSL

Can someone help me with this one please?
We have a XCart site running version 1.19.2.1 and we would like to force all traffic to HTTPS. The SSL certificate is installed and the .htaccess file looks like this:
DirectoryIndex home.php index.php index.html index.htm
# ErrorDocument 404 /404.php
# Show default error document for 404 errors caused by opening of image/media files.
<Files ~ "\.(gif|jpe?g|png|js|css|swf|ico)$">
ErrorDocument 404 default
ErrorDocument 401 default
# Clean URLs [[[
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain\.com
RewriteRule ^(.*) http://www.domain.co.uk/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteCond %{ENV:REDIRECT_STATUS} !^401$
RewriteCond %{REQUEST_URI} !^/(payment|admin|provider|partner)/
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ dispatcher.php [L]
</IfModule>
# /Clean URLs ]]]
RewriteEngine on
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
I have tried editing the file to redirect all traffic to HTTPS, I get a too many redirects error.
One example that I tried was replacing the entire contents of the .htaccess file with:
ErrorDocument 404 /index.php
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
rewritecond %{http_host} ^domain.co.uk [nc]
rewriterule ^(.*)$ https://www.domain.co.uk/$1 [r=301,nc]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R=301,L]
# RewriteRule ^([a-zA-Z0-9\-\%\s\_\.]+)(.htm)$ index.php?page=$1 [L]
##RewriteRule ^([a-zA-Z0-9\-\%\s\_\.]+)\.htm$ index.php?page=$1 [L]
DirectoryIndex index.php
#DirectoryIndex site_closed.html
RewriteEngine on
The above works fine on another XCart site but not this one. Again, I get the too many redirects error.
Anybody have any idea how I can fix it?
Kind regards
James
I had edited the htaccess file but I hadn't edited the https.php file as described here: https://help.x-cart.com/index.php?title=X-Cart:Configuring_HTTPS#Configure_the_HTTPS_server_in_X-Cart
All working now.

How to remove .php extension (NOTE: I have setup 301 redirect on trailing slash)

I am trying to figure out how to remove .php extension from my URL's.
I did have this setup, but recently I redirected my trailing slash URL's to clean URL's without it. i.e http://www.customlogoshop.com/logo-design/ goes to http://www.customlogoshop.com/logo-design.php
But the .php extension URL I do not want. I want it to just be /logo-design
Any help would be great please!
Options -Indexes
Options +FollowSymlinks
<Files .htaccess>
deny from all
</Files>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [L,R=301]
</IfModule>
remove the R=301 flag :
Options -Indexes
Options +FollowSymlinks
<Files .htaccess>
deny from all
</Files>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]

configuring .htaccess file after migration in expressionengine

So basically I've moved a site from the original top domain to a folder in my site but the page is blank due to the .htaccess not automatically redirect to /en/
I tried placed my domain, julianjosephs.com before the existing domain but it didn't work. Not sure what to do now.
Original: http://morganstachs.com/en/
Development: http://julianjosephs.com/morganstachs.com/
.htaccess file contents
# -- LG .htaccess Generator Start --
# .htaccess generated by LG .htaccess Generator v1.0.0
# http://leevigraham.com/cms-customisation/expressionengine/addon/lg-htaccess-generator/
# secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Dont list files in index pages
IndexIgnore *
# EE 404 page for missing pages
ErrorDocument 404 /index.php?/
# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>
RewriteEngine On
RewriteBase /
# remove the www
RewriteCond %{HTTP_HOST} ^(www\.$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Remove index.php
# Uses the "include method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(_includes|site|landing_pages|listings_shortcut|offers|scripts|xml|en|sp|fr|members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Remove IE image toolbar
<FilesMatch "\.(html|htm|php)$">
Header set imagetoolbar "no"
</FilesMatch>
# -- LG .htaccess Generator End --
RewriteCond %{HTTP_HOST} ^admin\.morganstachs\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.admin\.morganstachs\.com$
RewriteRule ^/?$ "http\:\/\/morganstachs\.com\:2082" [R=301,L]

How can I remove index.php using Codeigniter on XAMPP?

I can't hide Codeigniter index.php on XAMPP 1.7.3
URL:
http://localhost/Servidor/agentesRainbow/index.php/agentes/tony
tony is an argument
My actual .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Servidor/agentesRainbow/
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|css|public|)
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /application/errors/404.php
</IfModule>
My routes.php:
$route['agentes/(:any)'] = "agentes/index/$1";
$route['default_controller'] = "agentes";
$route['scaffolding_trigger'] = "";
My config.php:
$config['base_url'] = "http://localhost/Servidor/agentesRainbow/";
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
On httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory /Servidor/agentesRainbow/>
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Add this line to the REQUEST_FILENAME conditions:
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
Here is the updated .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Servidor/agentesRainbow/
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|css|public)
RewriteRule ^(.*)$ /Servidor/agentesRainbow/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /application/errors/404.php
</IfModule>
Hope it solves your problem!
I think the bar at the end of the rewrite condition is causing a problem. At least it broke the rewriting for me.
RewriteCond $1 !^(index\.php|images|css|public|)
Try removing the bar.
RewriteCond $1 !^(index\.php|images|css|public)
base_url should be just "http://localhost";
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|other|directories|you|want|accessible|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Resources