Reputation: 135
I am getting errors when I am trying to rewrite URL.
From this:
www.example.com/products/product-details.php?alias=abc-def-123
To this:
www.example.com/products/abc-def-123
Here is my .htaccess file
#Turn Rewrite Engine On
Options +FollowSymLinks -MultiViews
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([a-zA-Z0-9-]+)$ products/product-details.php?alias=$1 [NC,L]
</IfModule>
I am hitting this URL:
www.example.com/products/abc-def-123
Upvotes: 2
Views: 131
Reputation: 135
Yeah!!!!!!! I solved my problem myself and also I want to thanks @RavinderSingh13 to make effort.
Here is the whole .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteRule ^products/([^/\.]+)?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
Upvotes: 0
Reputation: 133508
With your shown samples please try following rules. Please make sure to keep your htaccess in your root folder along with your products folder(not inside it).
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteCond %{THE_REQUEST} \s/products/.*\?alias=(\S+)\s
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/(.*)/?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
Upvotes: 3