2

I discovered search engines found duplicate content on my domain due to URLs being output with index.php?p= and index.php in ADDITION to the clean URLs.

For example, I have:

  1. domain.com/index.php?p=about
  2. domain.com/index.php/about
  3. domain.com/about

And the first two requests with index.php?p= and index.php shouldn't exist. Only the last request with domain.com/about should exist.

While I found help removing index.php, some URLs continue to include ?p=.

So, now I still have:

  1. domain.com/?p=about
  2. domain.com/about

While all I want is:

  1. domain.com/about

Note: This is happening to several pages and blog entries throughout the site. Not only URLs directed to about.

How may I remove ?p= permanently from the remaining URLs?

Within .htaccess, I have:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Remove index.php
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !^/index\.php/admin\b [NC] # Don't mess with admin requests
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

Within craft/config/general.php, I have:

return array(
    '*' => array(

        'omitScriptNameInUrls' => true,
    ),
);

1 Answer 1

7

Taken from this answer on Stack Overflow: .htaccess mod_rewrite: rewriting querystring to path

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Remove index.php
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !^/index\.php/admin\b [NC] # Don't mess with admin requests
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Remove ?p=
    RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)p=([^&\s]+)&?([^\s]*)
    RewriteRule ^(.*)$ /%3?%1%4 [L,R=301]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

This not only reformats /?p=about as /about, but it also preserves other parts of the query string.

For example, /?p=about&tag=stuff and even /?tag=stuff&p=about both redirect to /about?tag=stuff.

2
  • If i use this then i cannot login to admin dashboard anymore.... Any idea?
    – Paulv12345
    Commented Aug 31, 2017 at 8:57
  • If you are using a different url other than /admin to access your control panel, you will need to update line 6 above and replace "admin" with your custom control panel trigger. RewriteCond %{REQUEST_URI} !^/index\.php/your-custom-cp-trigger\b [NC]
    – Alex Roper
    Commented Sep 1, 2017 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.