.htaccess, remove .php and add / to end of url

How do I get my urls to look like:

www.example.com/test/

instead of www.example.com/test.php

and to make it work in sub directories too like:

www.example.com/test2/file/

instead of www.example.com/test2/file.php

If you rename test.php to just ‘test’ and then make an .htaccess file with the following:

<Files test>
ForceType application/x-httpd-php
</Files>

It will work, the only trouble is that for each additional file that works like this you need to duplicate that part of the .htaccess

It depends how many files you want to handle like this really…

For two files you can do this:

<Files test>
ForceType application/x-httpd-php
</Files>
<Files test1>
ForceType application/x-httpd-php
</Files>

And for subdirectories you can just create a .htaccess in the subdirectory in the same way.

Hope that helps!

is it possible to remove the .php through the .htaccess instead of renaming each file?

A quick search of google for ‘clean url’ found a few things that might be useful:

http://wettone.com/code/clean-urls

http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

http://www.slipszenko.net/tutorial:Making+Clean+URLs

Hope that’s helpful :slight_smile:

I tried google but I can’t seem to find what I mentioned in my 1st post, the links you give do work but don’t add the / at the end of the url.

catch,

Best to have a read of the Article in my signature as it answers your questions.

Regards,

DK

dklynn,

I checked out your site so far I got in my .htaccess

RewriteEngine on
RewriteRule ^/?([a-z]+)$ $1.php [L]

now that works when I type

www.example.com/test

but when I type:

www.example.com/test/

it doesn’t, and I can’t find out how to add the / on the end of the URL… on your website.

catch,

If you use the trailing /, it can be confused as a directory request. However, mod_rewrite IS up to the task of handling either situation by adding an optional slash before the end anchor. Just be aware that EVERY subdirectory request without a file in the path will be treated this way, too! In other words, that’s NOT recommended.

RewriteEngine on
RewriteRule ^/?([a-z]+)/?$ $1.php [L]

Regards,

DK

alright, thanks :slight_smile:

Hi,

how i can rewrite my urls to static ones.
Basically i don’t know how to write regular expressions thats why i am writing down the url structure and the result i am looking for.

http://www.myDomain.com/se/search_re…?search=iphone

I want to make it:
http://www.myDomain.com/se/search/iphone

OR

More better if some one can give me a .htaccess file that will rewrite url to
http://www.myDomain.com/se/iphone

Thanks in advance.

fb,

That’s ALL in the tutorial Article linked in my signature.

Please read threads (in this thread, it’s post #6) and you’ll get that same advice.

Regards,

DK

Hi DK,

I read your tutorial and used the following code in my .htaccess

RewriteEngine on
RewriteBase /se

RewriteRule ^search/(.*)/?$ search_results.php?search=$1 [NC,L,QSA]

But still the url is not re-written.
Can you please help.

Thank

fb,

If you’d read it, you would know to NEVER use (.*) - it’s just lazy code which will backfire on you 99 times out of 100. Also, I hope that I’d stated in there that RewriteBase is ONLY to correct a Redirect problem. Saying that,

RewriteEngine on
RewriteRule ^/?se/search/([a-z]+)/?$ se/search_results.php?search=$1 [L]

Your optional trailing slash will NEVER be matched (because of the (.*)) and No Case and Query String Append flags are NOT appropriate here.

Regards,

DK

Hi DK,

I am a beginner and i am not sure where i am making mistake.

If you mean that i put the above code:
RewriteEngine on
RewriteRule ^/?se/search/([a-z]+)/?$ se/search_results.php?search=$1 [L]

in my .htaccess then i just did it but again it didn’t worked.

You can see the demo of my script at:
http://www.projectsupdates.com/shopping_api/

I will be thankful for your help in solving my problem.

Thanks

Sorry, my .htaccess file contains:

RewriteEngine on

RewriteRule ^/?shopping_api/search/([a-z]+)/?$ shopping_api/search_results.php?search=$1 [L]

fb,

I believe that your problem stems from not having an action for your form! As a POST method, the value of the search item will be hidden from Apache (merely passed through with the POST array and not subject to review by mod_rewrite.

IF you wanted to use the search term, you would have to use the GET method OR process the action in a JavaScript to create the desired URI format before SUBMIT.

Regards,

DK