Jump to content

.htaccess url rewriting


aspnetguy

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.

Link to comment
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".

Link to comment
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.

Link to comment
Share on other sites

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

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

Link to comment
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?

Link to comment
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.

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...