Apache URL Rewriting

Hi

I was wondering if the following are possible with .htaccess?

  1. Hide all .php file extensions. For example http://domain.com/login.php should look http://domain.com/login

  2. When I pass params like

http://domain.com/search?s=abc&d=11&v=hello

it should look like

http://domain.com/search/s/abc/d/11/v/hello

Please if someone can help me achieve the above?

Many thanks in advance

[FONT=Courier New]# If the requested URL isn’t a real file
RewriteCond %{REQUEST_FILENAME} !-f

Then rewrite to add a .php extension

RewriteRule (.*) $1.php[/FONT]

Off the top of my head, I’m not sure how you would do this one, and frankly, I don’t think you should try. Google actually recommends against this kind of URL format. Since /search/s/abc/d/11/v/hello is the same as /search/d/11/v/hello/s/abc, which is the same as /search/v/hello/s/abc/d/11, you’re going to end up with an unnecessarily high number of URLs that point to identical content.

Hi Thanks for sharing code.

This will be not for google indexing, it will be shown once user logs in, which has nothing to do with google indexing…

Thanks

c10,

While I would modify Jeff’s code to require only lowercase characters, that would have to be dependent upon your file names and whether you want to allow this for subdirectory requests. IMHO, it’s better to have mod_rewrite do a little error checking for you so I would also check to see whether the file exists WITH the .php extension before redirecting:

RewriteEngine on

# Check whether file exists
RewriteCond %{REQUEST_FILENAME} !-f
# Check whether request is a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CHECK WHETHER file exists with .php  extension
RewriteCond %{REQUEST_FILENAME}\\.php !-f
RewriteRule ^([a-z]+)$ $1.php [L]

One advantage of this is that the greedy problem disappears when you to do work on your second “problem.”

# Convert three key/value pairs in URI to query string
# Change character range definitions to include digits or CAPS if required (it is in your example)
RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ $1.php?$2=$3&#4=$5&$6=$7 [L]

Note that, to match d’s value of 11, you WILL have to change the value’s character range definition (at least once) to [a-z0-9]+ or simply [0-9]+, however, using character range definitions will help you get rid of garbage.

IF your key value pairs will ALWAYS be search, d and v IN THAT ORDER, then I would remove those character range definitions and replace with those values.

Alternatively, you could generate the problem Jeff mentioned (different URIs for same content) by simply listing the acceptable key names like (search|d|v). Not enough information to give a recommendation, though.

Regards,

DK

Hi Guys

Thanks for your inputs.

I am getting 404 Object no found error after I navigate to http://localhost/projects/login

i have already added the .htaccess file with the content shared by dklynn. .htaccess file is in the root of the “projects” directory

mod_rewrite is enabled.

What could be wrong?

It’s a lot easier to answer that if you show us the code you’re using now.

I am using this

RewriteEngine on
# Check whether file exists
RewriteCond %{REQUEST_FILENAME} !-f
# Check whether request is a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CHECK WHETHER file exists with .php  extension
RewriteCond %{REQUEST_FILENAME}\\.php !-f
RewriteRule ^([a-z]+)$ $1.php [L]

This is why. Note that when dklynn recommended matching only letters, he also said it depends on whether you want to allow for subdirectory requests. And it seems you do want to allow for subdirectory requests.

I’d recommend you change the pattern to FONT=Courier New[/FONT]. This pattern is standard practice, and it’s used even in the Apache documentation for exactly this kind of purpose.

I changed the content of .htaccess to the following still no dice

RewriteEngine on
# Check whether file exists
RewriteCond %{REQUEST_FILENAME} !-f
# Check whether request is a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CHECK WHETHER file exists with .php  extension
RewriteCond %{REQUEST_FILENAME}\\.php !-f
RewriteRule ^([.*]+)$ $1.php [L]

Remove the square brackets. And the plus sign.

It should be

RewriteRule ^(.*)$ $1.php [L]