Jump to content
Sign in to follow this  
aspnetguy

.htaccess url rewriting

Recommended Posts

I am trying to rewrite a url that might look like this www.mydomain.com/page.php?name=Joe&id=100 to look like this www.mydomain.com/engine/Joe/100.htmlI have the following .htaccess file. The first part tells all php pages to be processed by PHP5 so it is required.

AddHandler x-httpd-php5 .php# BEGIN WordPress<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPressOptions +FollowSymLinksRewriteEngine onRewriteRule engine/(.*)/(.*)\.html$ xuml.php?name=$1&id=$2

Am I even close? This is my first attempt at url rewriting for Apache.

Share this post


Link to post
Share on other sites

I thought you said you want to rewrite the URL www.mydomain.com/page.php?name=Joe&id=100. From your sample, it seems you're rewriting the URL www.mydomain.com/xuml.php?name=Joe&id=100. Oh well, let's suppose that is the name of the true page.I'm not seeing the use of this line:

RewriteRule . /index.php [L]

By default the directory index is index.php anyway, unless you were on some weird hosting plan. You can do a simple directory listing without mod_rewrite by simply using

<IfModule dir_module>	DirectoryIndex index.php</IfModule>

The condition being there only for graceful degradation.Also, the rewrite rule allows for empty strings. The following URL is possible

www.mydomain.com/engine//.html

I'd use "+" instead of "*" to avoid that, like so:

RewriteRule engine/(.+)/(.+)\.html$ xuml.php?name=$1&id=$2

btw, if you'll be masking PHP as HTML pages, you might want to turn the expose_php directive from php.ini to "off".

Share this post


Link to post
Share on other sites

yeah I grabbed the example from my page after writing the post, I should have matched up the page names. But it is correct though? It isn't working so I thought I must have it wrong.

Share this post


Link to post
Share on other sites

Try to add ^ at the beginning of the rule. That is:

RewriteRule ^engine/(.+)/(.+)\.html$ xuml.php?name=$1&id=$2

Share this post


Link to post
Share on other sites

doesn't seem to workI have this for my .htaccess file

AddHandler x-httpd-php5 .php# BEGIN WordPress<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPressOptions +FollowSymLinksRewriteEngine onRewriteRule ^engine/(.+)/(.+)\.html$ xuml.php?name=$1&id=$2

The page is working as you can test it here http://www.justinbezanson.com/dev/xuml/xum...=Joe&id=100 but http://www.justinbezanson.com/dev/xuml/engine/Joe/100.html cannotbe found.Any ideas?

Share this post


Link to post
Share on other sites

Try to simplify it a bit first and enhance it progressively.Remove those conditions. What's the point of them anyways?And make yourself fancier by using a folder (which though fancier is actually simpler) like so:

RewriteRule ^engine/(.+)/(.+) xuml.php?name=$1&id=$2

I'm not seeing much of a point to turn on the rewriting engine on twice btw. Once if the module is available and once anyways.

Share this post


Link to post
Share on other sites

this is the first time I have ever used a .htaccess file. How would you write it based on the code I showed above?

Share this post


Link to post
Share on other sites

I took out the first part of the file to simplify it. I just have

Options +FollowSymLinksRewriteEngine OnRewriteRule ^engine/(.+)/(.+) xuml.php?name=$1&id=$2

but it is still not working

Share this post


Link to post
Share on other sites

ok using this works for http://www.justinbezanson.com/dev/xuml/engine/Joe/100/ but it requires the / at the end or it fails.

Options +FollowSymLinksRewriteEngine OnRewriteRule ^engine/(.+)/(.+)/$ xuml.php?name=$1&id=$2

I tried using the below file to catch the url without the slash and first direct it with the slash but it is still not catching hte url without slash and is redirect wrong. Any ideas on that?

Options +FollowSymLinksRewriteEngine OnRewriteBase /dev/xuml/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^engine/(.+)/(.+)$ /engine/$1/$2/RewriteRule ^engine/(.+)/(.+)/$ xuml.php?name=$1&id=$2

Share this post


Link to post
Share on other sites

I don't know much about this but it does look like Regular Expressions which I do know about.Does apache allow the ? to specify that a string is optional? If so, you might try this for your rule:

RewriteRule ^engine/(.+)/(.+)/?$ xuml.php?name=$1&id=$2

Share this post


Link to post
Share on other sites

I don't know if it would work, but here's an interesting idea. Instead of using two variables, you can always pass one and perform negotiation further with PHP. This might be more comforting since PHP would be a more familiar environment. You can do that by passing $0 instead of $[1-9], like so:

RewriteRule ^engine/(.*) xuml.php?uri=$0

Everything written after "engine/" will be passed onto the uri variable. The following does the same, but it requires two sets to invoke the rewriting:

RewriteRule ^engine/(.+)/(.+) xuml.php?uri=$0

While $1 will reference "Joe" and $2 will reference "100", $0 will reference "Joe/100" which you can then disassemble in PHP.

Share this post


Link to post
Share on other sites

Turns out it is a problem with GoDaddy (who are blaming it on Apache). They are still using Apache 1.3.x and do not have mod_dir enable which auto adds the slash if it is missing. I have found some .htaccess code that will append the missing slash before I run my rule.I'll let you know if it works.

Share this post


Link to post
Share on other sites

I have come to the conclusion that GoDaddy just blows. No matter what gets put in the .htaccess file their setup of Apache tries to find the directory with the name of the piece with no slash.Blaming Apache is really lame other Linux host don't have this problem. Godaddy (and only Godaddy) keeps coming up in the Googel searchs I have done to try and fix this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×