.htaccess rewrite urls

Hi,

I would like to rewrite these urls (and others that look the same):
http://example.com/blog.php
http://example.com/test/pay.php

to:
http://example.com/blog/
http://example.com/test/pay/

and also make the urls work if the last forward slash is not added to the url, like:
http://example.com/blog >> http://example.com/blog/

Is this possible?

Thanks!
peps03Asked:
Who is Participating?
 
askurat1Connect With a Mentor Commented:
Sorry forgot to edit something:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>

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

  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^(.+)\.php$ /$1 [R=301,L]
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !\..+$
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L]

  RewriteRule ^(.*)/$ $1.php [L]
  
</IfModule>

Open in new window

0
 
askurat1Commented:
This is definitely possible.
0
 
peps03Author Commented:
sharp as a knife..

and now the real question.. ;)

how should that be done?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
askurat1Commented:
Something like this:
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^blog\.php$ "http\:\/\/www\.example\.com\/blog\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^test\/pay\.php$ "http\:\/\/www\.example\.com\/test\/pay\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^blog$ "http\:\/\/www\.example\.com\/blog\/" [R=301,L]

Open in new window

0
 
peps03Author Commented:
the site has about 30 pages, would i have to enter them all manually?

and can i exclude a folder from being re-written?

> the site is already redirected from www. to non-www.
0
 
askurat1Commented:
Well do you mean if you typed in blog.php or test.php they would be rewritten like blog/ and test/?
0
 
peps03Author Commented:
yeah, just remove the .php from all the site pages and replace it with a "/"
0
 
askurat1Commented:
Try this instead:
RedirectMatch 301 (.*)\.php http://www.example.com$1/

Open in new window

0
 
peps03Author Commented:
that isn't working, it redirects the site to:
http://example.com/index/ and then says: This webpage has a redirect loop
0
 
askurat1Commented:
Did you take out the rewrite I put above?
0
 
askurat1Commented:
Any way you can give me an example of your whole .htaccess file?
0
 
peps03Author Commented:
This is all that is in it:

Options +FollowSymlinks

RewriteEngine on

# www. to non www.
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]

#RedirectMatch 301 (.*)\.php http://example.com$1/

# to fix php basic authentication
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]

and some error page handling.
0
 
askurat1Commented:
try putting the redirectmatch at the very top.
0
 
peps03Author Commented:
Still same issue: This webpage has a redirect loop
0
 
askurat1Commented:
Thats strange. I tested the code above and it seems to work fine. Try just the one line I gave you and comment out the rest and see what you get.
0
 
peps03Author Commented:
still the same..
RewriteEngine on has to be on right?

could it by caused by the site/server running php as cgi or other way around?

i've had troubles with the site before. i had to add:
# to fix php basic authentication
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]

and some lines of php before the authentication lines, in order for the basic php authentication to work..
0
 
askurat1Commented:
I want you to comment out everything except: redirectmatch that includes commenting out rewriteengine on.
0
 
peps03Author Commented:
ok, did that, result:
http://example.com/index/

Not Found

The requested URL /index/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
0
 
askurat1Commented:
Well the problem seems to be you are trying to redirect a .php to a folder. Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Open in new window


It will not redirect if you type in .php after it but if you typed in blog it will redirect to blog/
It should run as you want.
0
 
peps03Author Commented:
aah i see. thanks. it works, but then the site is displayed without the css. what can be done about this?
0
 
peps03Author Commented:
links like this:
example.com/folder/blog
don't redirect to:
example.com/folder/blog/
but i get a 404 not found.
0
 
askurat1Commented:
So I have done some testing and there doesn't seem to be a way to use /blog/ and have it work correctly. I do have some code that will take out the php though. For instance if you typed in /blog it would just stay at /blog but if you typed in /blog.php it will redirect to/blog without .php
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
  DirectorySlash Off

  RewriteEngine On
RewriteCond %{THE_REQUEST} \ /(.+/)?\.html(\?.*)?\  [NC]
RewriteRule ^(.+/)?.html$ /%1 [R=301,L]

  RewriteCond %{SCRIPT_FILENAME}/ -d
  RewriteCond %{SCRIPT_FILENAME}.html !-f
  RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^(.+)\.html$ /$1 [R=301,L]

  RewriteCond %{SCRIPT_FILENAME}.html -f
  RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

</IfModule>

Open in new window

0
 
askurat1Commented:
Make sure to change .html to .php in my code.
0
 
askurat1Commented:
I think I have it figured out. Exclude the last code:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>

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

  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^(.+)\.html$ /$1 [R=301,L]
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !\..+$
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L]

  RewriteRule ^(.*)/$ $1.php [L]
  
</IfModule>

Open in new window

0
 
askurat1Connect With a Mentor Commented:
Damn I keep forgetting things. In your main file make sure to reference your css and js with the full path. For example use http://www.example.com/styles/main.css instead of styles/main.css
0
 
peps03Author Commented:
Great askurat1! Thanks.

I'm not home at the moment, will check it out first thing tomorrow morning!!
0
 
askurat1Commented:
No problem. Let me know if something isn't quite right. I tested this myself and it seems to work perfectly but everyones setup it different.
0
 
peps03Author Commented:
Thanks askurat1

Works great! Just like i wanted it!

Thanks a lot for your time!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.