Jump to content

Recommended Posts

At the moment I have the following .htaccess setup:

 

mysite.com/contact

it goes to

mysite.com/contact.php

 

Which is simple, but how would I do it so it also works with a trailing slash, e.g.:

 

mysite.com/contact/    and mysite.com/contact

goes to

mysite.com/contact.php

 

Then how would I make it so folders like /images/ or /js/ or /inc/ etc aren't confused as .php files?

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/178929-clean-but-clever-urls/
Share on other sites

You listed what your .htaccess file does, but not what it looks like... to work with the trailing slash you would try something along the lines of

 

^mysite.com/([^/])/?$ /mysite/$1.php

 

With regards to not forwarding when the file actually exists you would use a RewriteRule such as...

 

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

My .htaccess is this at the moment:

 

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [L,QSA]

 

But it doesn't work if there's a trailing slash, it just brings up a Internal Server Error 500.

 

I don't quite understand your line?

  • 2 weeks later...

Wahey, that works! Only problem is with a trailing slash, /contact/ it gets confused with where the images/css files are - probably because it thinks its a folder.

 

Anyway around this?

 

Thanks alot! :)

 

got the same problem which i'm looking for a solution.

  • 1 month later...
  • 2 weeks later...

fixed my problem by adding <base href="http://www.mydomain.com" />  to html

 

Thanks regiemon! That's come in handy.

 

 

I'm having issues with using this .htaccess in subdirectories however, it trys to use the root domain, rather than the subdirectory, for example:

 

mysite.co.uk/test/site/about

 

Trys to goto

 

mysite.co.uk/about

 

I did this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite.co.uk/test/site/([^/]+)/?$ /$1.php

 

Which goes to the correct URL but does a "Not found" error unless I include the *.php extension.

 

Any ideas? Thanks alot and happy holidays!

 

 

 

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.