skip to Main Content

First time using mod_rewrite here I have this rules working on IIS:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="services" stopProcessing="true">
                    <match url="^([a-z]+)/service/([a-zA-Z-]+)" />
                    <action type="Rewrite" url="serveis.php?id={R:2}&amp;lang={R:1}"/>
                </rule>
                <rule name="categories" stopProcessing="true">
                    <match url="^([a-z]+)/category/([a-zA-Z-]+)" />
                    <action type="Rewrite" url="subhome.php?id={R:2}&amp;lang={R:1}"/>
                </rule>
                <rule name="index_idioma" stopProcessing="true">
                    <match url="^([a-z]+)/index" />
                    <action type="Rewrite" url="index.php?lang={R:1}"/>
                </rule>
                 <rule name="index" stopProcessing="true">
                    <match url="http://www.domain.com" />
                    <action type="Rewrite" url="http://www.domain.com/index.php?lang=en"/>
                </rule>
                  <rule name="kinds" stopProcessing="true">
                    <match url="^([a-z]+)/kind" />
                    <action type="Rewrite" url="tipusserveis.php?lang={R:1}"/>
                </rule>
                <rule name="company" stopProcessing="true">
                    <match url="^([a-z]+)/our_company" />
                    <action type="Rewrite" url="/empresa.php?lang={R:1}" />
                </rule>
                 <rule name="contact" stopProcessing="true">
                    <match url="^([a-z]+)/contact" />
                    <action type="Rewrite" url="/contacto.php?lang={R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

And now I want to make it using apache’s mod_rewrite I tried this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/([a-z]+) index.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/our_company/?$ empresa.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/contact/?$ contacto.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/service/([A-Za-z0-9-]+)/?$ serveis.php?id=$2&lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/category/([A-Za-z0-9-]+)/?$ subhome.php?id=$2&lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/kind/([A-Za-z0-9-]+)/?$ tipusserveis.php?id=$2&lang=$1 [NC,L]
</IfModule>

but none of them is working.

Can somebody help me here, atleast give me some directions on how I should do it?

EDIT:
Plesk Panel automatically adds this to httpd.conf

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com$1 [L,R=301]
<IfModule>

I guess it removes the www from the URL but I don’t know if it also stops the other rules to be applied.
Thanks

2

Answers


  1. Your are getting errors in mod_rewrite due to missing anchors ^ and $ in your URI patterns. Try these rules in your root .htaccess or in Apache config:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^ - [L]
    
        RewriteRule ^/?([a-z]+)/?$ index.php?lang=$1 [NC,L,QSA]
        RewriteRule ^/?([a-z]+)/our_company/?$ empresa.php?lang=$1 [NC,L,QSA]
        RewriteRule ^/?([a-z]+)/contact/?$ contacto.php?lang=$1 [NC,L,QSA]
        RewriteRule ^/?([a-z]+)/service/([a-z0-9-]+)/?$ serveis.php?id=$2&lang=$1 [NC,L,QSA]
        RewriteRule ^/?([a-z]+)/category/([a-z0-9-]+)/?$ subhome.php?id=$2&lang=$1 [NC,L,QSA]
        RewriteRule ^/?([a-z]+)/kind/([a-z0-9-]+)/?$ tipusserveis.php?id=$2&lang=$1 [NC,L,QSA]
    </IfModule>
    
    Login or Signup to reply.
  2. 1) Your subsittution (2nd parameter) must not be a relative path. It needs to either start with a / or be a redirect, never just e.g. “index.php”. This is the direct cause of your 400s — your error log probably clearly showed a URL-path not starting with / (which is invalid)

    2) You should ignore the suggestions to put these rules in .htaccess. This makes mod_rewrite much more complex to maintain.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search