Apache RewriteCond tweaks

Hello there

I’m trying to make a multi language website and I was looking for some help to setup pretty urls ( for seo ) on my site.

I have the following rule:

<IfModule mod_rewrite.c>
RewriteRule ^empresa/?$ /empresa.php [L]
</IfModule>

When I press http://www.mysite.com/empresa/ it passes the request for empresa.php like it should do.
For the english version of the page I have this:

<IfModule mod_rewrite.c>
RewriteRule ^empresa/?$ /empresa.php [L]
RewriteRule ^en/empresa/?$ /en/empresa.php [L]
</IfModule>

This for each language would not do as I have several redirects like these all over the .htaccess and it keeps getting bigger.

/en/ is a physical directory on the site structure ( keeping my designer team happy ) and I would like to detect the ‘/en/’ ( from http://www.mysite.com/en/empresa/ ) setting a variable as the base rewrite and apache would use it to forward request accordingly.

Like so:

<IfModule mod_rewrite.c>
RewriteCond %SOMEVAR ^/en/
RewriteRule %SOMEVAR ^empresa/?$ %SOMEVAR/empresa.php [L]
</IfModule>

This would save some lines on my htaccess.

Thank you for your help.

Hello dklynn

Yes. You quite right in that comparison.
I did thought of that also and will not make that mistake again xD
I’ll keep in mind also for other tests and avoid them in the future.

Thanks again :smiley:

Regards
Pedro

You don’t need a RewriteCond for that. Just make en/ in the RewriteRule:


RewriteRule ^(en/)?empresa/?$ /$1empresa.php [L]

That covers the functionality of both lines in your code :slight_smile:

Hello ScallioXTX

I shall try it.

Many thanks :smiley:

Regards

wi,

Once you’ve decided that the mod_rewrite works, REMOVE the <IfModule> test as that’s ABUSIVE of the server.

Regards

DK

Thanks dklynn

It should work but Gmetrix does not show any relevant gain towards speed.
I will check this with Firebug :smiley:

Many thanks to you all :smiley:

Best regards
Pedro

Hi Pedro,

Think of the <IfModule> test as something which should be done ONCE (i.e., reserved for the server configuration file), NOT for every file request. I liken that to the definition of an idiot as someone who repeats the same test expecting a different result every time. Best to test that the module exists ONCE then remove it from the .htaccess to avoid the abuse of the server.

Regards,

DK