Question

Im trying to make it so everything with news.php in the url gets redirected to a url which strips out the news.php and adds a trailing slash. So http://www.example.com/news.php/news/article to http://www.example.com/news/article/

Ive been able to remove the news.php with this

RewriteRule  ^(news|health|tips|weight|P[0-9]{2,8})$ $1/ [NC]
RewriteCond $1 ^((news|health|tips|weight|P[0-9]{2,8})/) [NC]
RewriteRule ^(.*)$ /news.php/$1 [L]

Ive tried adding a trailing slash to the above line and it does nothing. I have also tried the stuff below, which adds a slash but doesnt remove the news.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /news.php/$1/ [R=301,L]

Can someone help me out here Im not sure what to do?

I also have some other rules

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteCond %{REQUEST_FILENAME}.php -f  

RewriteCond %{QUERY_STRING} ^(ACT=.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(URL=.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(CSS=.*)$ [NC]   
RewriteRule ^$ /news.php\?&%{QUERY_STRING} [L] 

RewriteRule ^(.*)$ $1.php [L,QSA]     
RewriteCond %{HTTP_HOST} !^www2\. 
RewriteRule ^(.*)$ http://www2.%{HTTP_HOST}/$1 [R=301,L]
Was it helpful?

Solution

You need an additional rule:

RewriteEngine On

RewriteCond %{THE_REQUEST} /news\.php/\S+ [NC]
RewriteRule ^news\.php/(.+?)/?$ /$1/? [R=301,L]

RewriteCond $1 ^((news|health|tips|weight|P[0-9]{2,8})/) [NC]
RewriteRule ^(.+)$ /news.php/$1 [L]

EDIT: To add a trailing slash to all non-files:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

OTHER TIPS

Ok so I changed the first block anubhava suggested to

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

and now everything seems to be working as I want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top