TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Mod Rewrite Question (http://www.talkphp.com/general/5198-mod-rewrite-question.html)

tego10122 12-29-2009 09:53 PM

Mod Rewrite Question
 
Code:

RewriteEngine On
RewriteBase /jay/admin

RewriteRule ^(.*)$ $1.php

I need it to basically do the following

when a user goes to www.domain.com/jay/admin/viewUsers it will call viewUsers.php

delayedinsanity 12-29-2009 10:22 PM

Give this a try,

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]


The RewriteCond tells mod_rewrite to ensure that we're not appending the file extension to a directory. The RewriteRule looks for a string that does not already have a period in it, denoting the probability of a file extension. This can be easily modified to allow periods, but check for a three character extension, or to any variety of test situations you would prefer.

Parvus 12-29-2009 11:04 PM

" www.domain.com/jay/admin/viewUsers it will call viewUsers.php "

The first question I would ask is, to what do you want to redirect exactly ?
www.domain.com/jay/admin/viewUsers -> www.domain.com/jay/admin/viewUsers.php
or would you like:
www.domain.com/jay/admin/viewUsers -> www.domain.com/viewUsers.php

Let's asume you mean case 1.
The easiest would be:
Code:

RewriteEngine On
^(http://www.domain.com/jay/admin/viewUsers)$ $1.php

But let's make it a bit cleaner than that =P
Code:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
^(.+)/viewUsers$ $1/viewUsers.php
# or even better:
#^(.+)/(\w+)/?$ $1/$2.php
#/? means that /viewUsers/ will also be redirected (notice the / on the end)

There are loads of combinations possible, it all depends on what you exactly need.
If you only need to redirect 1 specific url the upper code will do just fine. Want to redirect more url's who have the same base ? Than the RegEx will help you =]

delayedinsanity 12-29-2009 11:27 PM

I guess I assumed he was just hiding the extension. Didn't dawn on me to ask if it was just for the one file. :)

tego10122 12-30-2009 02:27 AM

he first question I would ask is, to what do you want to redirect exactly ?
www.domain.com/jay/admin/viewUsers -> www.domain.com/jay/admin/viewUsers.php

delayedinsanity 12-30-2009 03:11 AM

RewriteEngine On
RewriteBase /jay/admin

RewriteRule ^viewUsers$ viewUsers.php [NC,L]

You might find this useful too.


All times are GMT. The time now is 03:49 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0