0

I've tried searching and found nothing that helps me solve the following problem. I found this link on SO: https://stackoverflow.com/questions/1426056/good-htaccess-mod-rewrite-url-rewriting-tutorial but all of the links within it are 404 dead.

I'm trying to beautify the urls of my site so www.mysite.com/aboutus.php is rewritten to look like www.mysite.com/aboutus which works.

But I want to not do this on the admin/cms directory as it stops the CMS from working. So ignore www.mysite.com/admin.php

The code I'm using is:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

I don't think I'm doing this correctly as it also takes the www off of the start of the URL.

I have also found this SO: https://stackoverflow.com/a/9565288/1343827 which seems to do what I want, but I'm not 100% sure it will do what I want.

If anyone can help me rewrite this mod so it removes .php from the end of links, but ignores this in the admin directory that'd be great.

Community
  • 1
  • 1
GT22
  • 503
  • 1
  • 8
  • 25
  • possible duplicate of [.htaccess mod\_rewrite - how to exclude directory from rewrite rule](http://stackoverflow.com/questions/1848500/htaccess-mod-rewrite-how-to-exclude-directory-from-rewrite-rule) – Armel Larcier Mar 02 '14 at 22:12

1 Answers1

1

I don't think I'm doing this correctly as it also takes the WWW off the start of the URL.

This is happening because the URLs that you are redirecting to have the www removed:

# no "www." here -------------v
RewriteRule ^([^/]+)/$ http://mysite.com/$1 [R=301,L]

Try changing them all to:

RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

I have also found this SO: https://stackoverflow.com/a/9565288/1343827 which seems to do what I want, but I'm not 100% sure it will do what I want, so a tutorial is probably best (and I hate to be mooching of you guys all the time while I'm learning).

That link does what you want, you just don't need the profile.php stuff at the bottom, you can omit that. But this bit:

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

before your last rule is important (you've seemed to have left that out). It checks to see if the file exists with the php extension instead of blindly rewriting it (which will cause a 500 server error (See: https://stackoverflow.com/a/12463742/851273).

As for good tutorials. Most all the tutorials that I've seen are very general, they go over directives and rules in general as well as some of the more common applications. This is probably the best one that I've seen: http://www.modrewrite.com/

There's going to be a lot of specifics and quirks about mod_rewrite that isn't going to be covered in tutorials, and some of them will seem very counter-intuitive. You'll just have to keep using it and run into those situations, and learn from them.

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220