Gaconfig url builder bad impact on removing file extensions

Hi All,

I have added some rewrite rules to remove file extension in configuration file. It is working. I can see proper page displayed with no file extension.

However, I am also using gaconfig URL builder (https://gaconfig.com/google-analytics-url-builder/). Here when I put my url (with no file extension) like http://mysite.com/mypage and provide parameters for campaign source, medium, company, etc. I got new URL like http://mypage.com/mypage?source=email&medium=a
When I hit this new URL I got 404 Not Found error.
But when I try with url using file extension, I got the proper page.

Please help me with this.

Thanks

I think we should see your rewrite rules to be able to help

Hi,

Thanks for reply. Here is the rewrite rule.

#remove file Extension
#Redirect extension requests to avoid duplicate content
RewriteRule ^([^?]+)\.html$ $1 [NC,R=301,L]

#Internally add extensions to request
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*) $1.html [L]

Please help me with this.

Any one help please on this?

any help please.

Your help will be highly appreciated.

Hi b1712,

Sorry, I’m late to the party (as usual).

What you’ve created is a loop with the two RewriteRules returning code to active the other. Missing is a line which identifies a {REQUEST_URI} as having been redirected already to prevent the loop (the NC - No Case - flag is also a problem). Try:

[code]#remove file Extension
#Redirect extension requests to avoid duplicate content ONLY if not already redirected
RewriteCond %{IS_SUBREQ} false
RewriteRule ^(.+).html$ $1 [R=301,L]

#Internally add extensions to request
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*) $1.html [L][/code]

You had done an excellent job with your code … it took me years to find the {IS-SUBREQ} variable but it works a treat!

Regards,

DK

Hi DK,

Thank you for the reply.
I tried your code. However, it is not working. The results are same.

If I put extension and then query string og GA Config URL, then it gets redirected to proper page and without extension, I am getting Page Not Found error.

Could you please help me? I am triyng this for so many days but no luck so far.

Hi Barbara,

#remove file Extension
#Redirect extension requests to avoid duplicate content ONLY if not already redirected
RewriteCond %{IS_SUBREQ} false
RewriteRule ^(.+)\.html$ $1 [R=301,L]

#Internally add extensions to request
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule .? %{REQUEST_URI}.html [L]

I changed the code slightly (because you don’t need your ^(.*) to capture the {REQUEST_URI}). This code does exactly what you want, i.e., if there had been no internal redirections, the .html is stripped from the {REQUEST_URI} and displayed in the location box (by the R=301) THEN, if the (new) {REQUEST_URI} exists WITH A .html FILE EXTENSION, it will redirect to that html file.

Since this is NOT working, there is something else interfering with your mod_rewrite, probably your GA Config’c code. Please post your entire (mod_rewrite portion) of your .htaccess file and I’ll try to combine (or reorder) as necessary for you to make this work.

Regards,

DK

Hi DK,

I am using IIS6 and for redirection I am using IIRF.ini file where I have written my rewrite rules. Here is the rules in the file.

RewriteEngine On
RewriteCond %{IS_SUBREQ} false
RewriteRule ^(.+).html$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*) $1.html [L]

RewriteLog “/rewrite.txt”
RewriteLogLevel 5

Hi Barbara,

Uh, oh! I see the problem! IIS uses one of two M$-peculiar coding languages for its implementation of rewrite directives (only one is relatively similar to Apache’s mod_rewrite). Please note the keyword “relatively” because M$ did not duplicate Apache’s complete functionality with it’s mod_rewrite implementation … and I must guess that Apache’s {IS_SUBREQ} variable is one which is missing.

Years ago, when I realized that mod_rewrite code could be written to NOT loop on a circular redirect, I first created code to mark the redirection with an unique query string. I believe that you MIGHT be able to do that, too, using code like:

[code]RewriteEngine On
# the query string contains "redirect=true"
RewriteCond %{QUERY_STRING} redirect=true
# strip the .html file extension and redirect showing the new {REQUEST_URI}
RewriteRule ^(.+).html$ $1 [R=301,L]

# then, if the new {REQUEST_URI} (via {REQUEST_FILENAME} exists with an html extension
RewriteCond %{REQUEST_FILENAME}.html -f
# redirect the {REQUEST_URI} to {REQUEST_URI}.html (internally)
# I still don't like (.*) for other reasons but IIS may not like {REQUEST_URI}
# ... but use the end anchor, too!
# ( the $ after ^(.*) )
RewriteRule ^(.*)$ $1.html?redirect=true [L][/code]

Please let me know if that words for you as I will not use M$ online but should know when something does work for those in the M$ world. Thank you.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.