Ad

HTACCESS RewriteRules With Pretty Urls For SEO (without Allowing Access To Physical File Location)

I have been searching around and I cant seem to find an exact answer to my problem. For this example i will use domain.com as my domain name.

I have htaccess rules in place to redirect pretty urls to actual pages within the web server. Just like this

RewriteEngine on
RewriteBase /

...

RewriteCond %{QUERY_STRING} .+
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING}
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1

As you can see, this basically loads the /pages/about.php file when accessing domain.com/en/about-us url. This works just as it should.

My issue comes from when someone is trying to access the url directly by going to domain.com/pages/about.php, i dont want that to be a possibility or to affect my SEO by basically saying there are 2 URLs for the same page. So i read that by doing something like the code below would work. BUT its actually just keeping the URL that was entered and not doing what i need it to.

RewriteCond %{THE_REQUEST} \s/about\.php\?lang=en\s [NC]
RewriteRule ^ /en/about-us? [R=301,L]

RewriteRule ^en/about-us$ /pages/about.php?lang=en [L]

Any help or clarification is greatly appreciated

EDIT

I have also tried this below, but it obviously just does an endless loop adding the lang to the query string.

RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en
RewriteRule ^ en/about-us [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos [R=301,L]

RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [L]
Ad

Answer

1st solution: With your shown attempts/samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON

##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]

##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]

##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]

##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1 [NC,QSA,L]


2nd solution: OR try following htaccess rules set with using THE_REQUEST variable. Make sure either use above OR following rules only one at a time.

RewriteEngine ON

##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/pages/about\.php\?lang=en\s [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]

##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/pages/about\.php\?lang=fr\s [NC]
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]

##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]

##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [NC,QSA,L]
Ad
source: stackoverflow.com
Ad