peps03
asked on
.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!
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!
This is definitely possible.
ASKER
sharp as a knife..
and now the real question.. ;)
how should that be done?
and now the real question.. ;)
how should that be done?
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]
ASKER
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.
and can i exclude a folder from being re-written?
> the site is already redirected from www. to non-www.
Well do you mean if you typed in blog.php or test.php they would be rewritten like blog/ and test/?
ASKER
yeah, just remove the .php from all the site pages and replace it with a "/"
Try this instead:
RedirectMatch 301 (.*)\.php http://www.example.com$1/
ASKER
that isn't working, it redirects the site to:
http://example.com/index/ and then says: This webpage has a redirect loop
http://example.com/index/ and then says: This webpage has a redirect loop
Did you take out the rewrite I put above?
Any way you can give me an example of your whole .htaccess file?
ASKER
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.
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.
try putting the redirectmatch at the very top.
ASKER
Still same issue: This webpage has a redirect loop
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.
ASKER
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..
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..
I want you to comment out everything except: redirectmatch that includes commenting out rewriteengine on.
ASKER
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.
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.
Well the problem seems to be you are trying to redirect a .php to a folder. Try this:
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.
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]
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.
ASKER
aah i see. thanks. it works, but then the site is displayed without the css. what can be done about this?
ASKER
links like this:
example.com/folder/blog
don't redirect to:
example.com/folder/blog/
but i get a 404 not found.
example.com/folder/blog
don't redirect to:
example.com/folder/blog/
but i get a 404 not found.
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>
Make sure to change .html to .php in my code.
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great askurat1! Thanks.
I'm not home at the moment, will check it out first thing tomorrow morning!!
I'm not home at the moment, will check it out first thing tomorrow morning!!
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.
ASKER
Thanks askurat1
Works great! Just like i wanted it!
Thanks a lot for your time!
Works great! Just like i wanted it!
Thanks a lot for your time!