简体   繁体   中英

.htaccess Redirect Loop, adding multiple .php extensions

I have sort of a small parent/teacher social network set up for my school. I use my .htaccess file to make the links to teacher profiles cleaner and to add a trailing slash to all urls. I get this problem when going to /teachers/teacher-name/ the link (sometimes) redirects to /teachers/teacher-name.php.php.php.php.php.php.php.php... Below is my .htaccess file. Sometimes if I clear my browser cache in Chrome it temporarily fixes it. I can't exactly wright .htaccess syntax, but I'm pretty familiar with it. Any suggestions are appreciated!

RewriteEngine on
RewriteBase /

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]

#other rewrites
RewriteRule ^teachers/([^/\.]+)/$ /teachers/profile.php?u=$1
RewriteRule ^teachers/([^/\.]+)/posts/$ /teachers/posts.php?u=$1
RewriteRule ^teachers/([^/\.]+)/posts/([^/\.]+)/$ /teachers/post.php?u=$1&p=$2

RewriteRule ^gallery/([^/\.]+)/$ /gallery/album.php?n=$1
RewriteRule ^gallery/([^/\.]+)/slideshow/$ /gallery/slideshow.php?n=$1
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /gallery/photo.php?a=$1&p=$2&e=$3

EDIT :I have attached a screenshot of exactly what I'm talking about. 在此处输入图片说明

It should be because of your decision to add the trailing slash:

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]

Instead of this you may like to write this:

#force trailing slash/
RewriteCond %{REQUEST_FILENAME} !-f
#the following rule only works for .php obviously, so add other rules
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)([^/])$    /$1$2/ [L,R=301]

And because you used the 301 Redirection, the browser is most likely to cache the redirection, so you may need to clear the browser cache before it works.

Add a [L] to the end of your .php rules to assure mod_rewrite stops processing.

Often when you find that kind of a match for a rule, it is the only one you want to run on your request. Your last six rules are examples of this, also, except you can do without it on them because they do not overlap each other.

Looking at this again, I see a different error. You apparently have two rules trying to share conditions. This does not work. Each rule must have its own conditions

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php #this rule will always run

should be changed to

#remove php ext
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php #this rule will always run

beyond that, though, are you trying to add .php to URLs ending in / ?

RewriteRule ^([^/]+)/$ $1.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM