How to rewrite categories url in htaccess or urlManager for yii2 classified ads website?

263 Views Asked by At

I am having issues with duplicated pages for seo , i think that the website when i make a categoy creates an original url then creates a duplicate of that url with the slug ,because i will find myself with 2 url that point to same category one is with slug (seo-frindly) and the other one without !

When the user click on a category it will get the seo-friendly url ,but since the other url is still working ,even if it's not displayed anyware,google detects it somehow and shows that i have duplicates

I would like to rewrite urls from

example.com/category?slug=categoryName

to

example.com/category/categoryName

Any suggestions will be very appreciated !!

I tried many codes in htaccess but with no result ,unfortunatelly i am not familiar with php or yii2 framework and didn't even tried to modify urlManager in web.php to avoid making more damage than good :)

This is my actual htacces code :

#Redirect www to non-www 

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# BEGIN rewrite rules
<IfModule mod_rewrite.c>
    RewriteBase /
               
 # ADMIN APP
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteCond %{REQUEST_URI} ^/admin(/.*)?$
    RewriteRule admin/.* admin/index.php            
   
 # FRONTEND APP
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteRule . index.php
</IfModule>
# END rewrite rules

<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
        # mod_headers, y u no match by Content-Type?!
        <FilesMatch "\.(gif|png|jpe?g|svg|svgz|ico|webp)$">
        SetEnvIf Origin ":" IS_CORS
        Header set Access-Control-Allow-Origin "*" env=IS_CORS
        </FilesMatch>
    </IfModule>
</IfModule>

And this is my urlManager code :

urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
                'search' => 'site/search',
                'contact' => 'site/contact',
                '<controller:conversation>/<action:delete>' => '<controller>/<action>',
                '<controller:conversation>/<action:reply>/<conversation_uid:[a-z0-9_\-]+>' => '<controller>/<action>',
                '<controller:account>/<action:invoices>/<page:\d+>' => '<controller>/<action>',
                '<controller:account>/<action:conversations>/<page:\d+>' => '<controller>/<action>',
                '<controller:listing>/<action:index|update|package|preview>/<slug:[a-z0-9_\-]+>' => '<controller>/<action>',
                'page/<slug:[a-z0-9_\-]+>' => 'pages/index',
                '<controller:category>/<action:location|map-view|get-map-location>' => '<controller>/<action>',
                [
                    'pattern' => 'category/<slug:[a-z0-9_\-]+>/<page:\d+>',
                    'route' => 'category/index',
                ],
                [
                    'pattern' => 'category/<slug:[a-z0-9_\-]+>',
                    'route' => 'category/index',
                ],
                [
                    'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>/<page:\d+>',
                    'route' => 'category/map-view',
                ],
                [
                    'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>',
                    'route' => 'category/map-view',
                ],
                [
                    'pattern' => 'store/<slug:[a-z0-9_\-]+>/<page:\d+>',
                    'route' => 'store/index',
                ],
                [
                    'pattern' => 'store/<slug:[a-z0-9_\-]+>',
                    'route' => 'store/index',
                ],
                '<url:.+/>' => 'site/redirect'
            ],
1

There are 1 best solutions below

3
Raul Sauco On

Enable strict parsing on the configuration.

If you change the enableStrictParsing line the urlManager configuration to

'enableStrictParsing' => true,

The requests that don't use one of the URLs defined on the configuration will receive 404 as response, so the duplicate URLs like example.com/category?slug=categoryName will not be any longer valid.

The Yii2 docs for that method say:

Whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route. This property is used only when $enablePrettyUrl is true.

Keep in mind that you will need to define rules for all URLs that need to be accessible, that means, for example, that example.com will not be directed by default to site/index and will instead return a 404.

An example rule that would fix that would be

'/' => 'site/index',

The Yii2 docs have a very detailed article about routing that includes an extensive section on creating rules.