.htaccess redirect loop error - php

I created a .htaccess with the following goals:
1: Remove .php and force add trailing slash
2: Make http://website.com/sticker/fruit/apple = sticker.php?category=fruit&alt=apple
That works, but whenever I type something that does not exist, for example:
http://website.com/sdgsdg/ OR
http://website.com/dssdg/sdgsg/zkzzv/
I get redirect errors.
Here is my .htacess:
Options +FollowSymlinks
RewriteEngine on
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php/$ http://website.com/$1/ [L,R=301]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ http://website.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://website.com/$1/ [L,R=301]
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]
Can anyone spot whats causing the redirect loops?
Thank you in advance.

I found a solution that worked for me.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]

Related

Ignore a particular folder in htaccess

I am facing issues regarding my .htaccess. The .htaccess works, but it causes a problem with the admin folder. Would it be possible to exclude that specific folder from this .htaccess? Here is my .htaccess. It is outside the admin folder.
Options -MultiViews
ErrorDocument 404 http://localhost/blo-prt/err.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blo/([^/\.]+)?$ blo-det.php?postlist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/blo/([^/\.]+)?$ blo-prior.php?bloglist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tep/([^/\.]+)?$ temp-det.php?telist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/tep/([^/\.]+)?$ temp-of-prior.php?tlist=$1 [L,QSA]
You should add this to your RewriteConds:
# if request is not for the /admin/
RewriteCond %{REQUEST_URI} !^/admin/ [NC]
Personally, I use this code to remove the .php extension:
# enable url rewriting
RewriteEngine On
# remove .php
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Also, you should only type RewriteEngineOn just once at the top of the file.

how to remove question mark from two php URLs in .htaccess

I want to remove question mark from two URLs like this:
URL 1
http://localhost:8888/events/event.php?id=222
to
http://localhost:8888/events/event/222
URL 2
http://localhost:8888/membership/register.php?id=162
to
http://localhost:8888/membership/register/162
i'm use this code in .htaccess and just the 'event' page work:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
You can use:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite from pretty URL
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
# Rewrite with .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

Removing .php extension in URLs except sub directory

Hello I used below code to remove extension .php from url in .htaccess
index.php -------- index and it worked fine.
Please assist me to stop this from removing the .php extension in sub directories eg: www.example.com/cp/index instead of: www.example.com/cp/index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [NC,L,QSA,R=301]
The following did not work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)$ %1.php [L]
Can I the exception directory one specific call cp
You can use the following :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
#####exclude /cp folder####
RewriteCond %{REQUEST_URI} !^/cp
#################
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /((?!cp)[^.]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [NC,L,QSA,R=301]

.htaccess add / support

RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)/$ books.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)$ books.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/ books.php [QSA]
Everything works when I type: localhost/name
but breaks if I type: localhost/name/
What's wrong with my .htaccess and how do I add support "/"
You don't need 2 rules and your 2nd rule has invalid regex anyway. Try this rule:
Options -MultiViews
RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^books/([^/]+)/?$ books.php?id=$1 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/myproject/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]

.htaccess for multiple rewrite rules

I wanted to create an htaccess file to remove the .php extension and change course.php?id=1 to course/1. But that does not seem to work out. Can any one correct my htaccess?(I am quite new to writing htaccess files)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
# If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/course/(.*)$ /course.php?id=$1 [NC]
Options -Indexes
Keep it this way:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# add a trailing slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteRule ^course/([^/]+)/?$ course.php?id=$1 [NC,L,QSA]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php [L]

Categories