.htaccess redirect with php slug - .htaccess

My rewrite code is:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ products.php?slug=$1
But, this shows and internal server error for the products/slugname rewrite rule.
I've only used small letter, numbers and dash(-) in my slug id.

Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ /products.php?slug=$1 [NC,L]

Related

Add slashes to .php file extensions only

my htaccess codes add a slash to the end of all URLs but I just want it to add slashes to files ending with the .php file extension how can I do that?
my htaccess codes :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Could you please try following. Written with shown samples only. Please make sure to clear browser cache before testing your URLs
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/seo$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

How to redirect a URL and then rewrite it to same url with htaccess but from the root?

This code below for rewrite/redirect from localhost/moneyworld/exchange?title=BTC_PMUSD to localhost/moneyworld/BTC_PMUSD but i want to rewrite/redirect from www.domain.com/exchange?title=BTC_PMUSD to www.domain.com/BTC_PMUSD Without also /moneyworld/
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(moneyworld)/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1/%2 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /moneyworld/exchange?title=$2 [L]
Thanks
The problem was that i was forwarding exchange.php to exchange after the url rewrite rules so simply the code below solved it
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /exchange?title=$1 [L]

htaccess rewrite get url section not working

I am trying to rewrite the url example.com/parts/toolbucket/part.php?id=1 into example/toolbucket/part/1
I have most of the .htaccess working but the last part of the rewrite doesn't work.
This is my .htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/parts/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /parts/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
Any ideas?
Because you are missing the directory part in the rule. If you want your URL to be this example.com/toolbucket/part/1
Replace this
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
With this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^toolbucket/part/([0-9]*)/?$ /parts/toolbucket/part.php?id=$1 [L]

htaccess seo-friendly URL's

I need some help to get friendly URL's on my website.
These are my links i need to rewrite:
www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number
This is my .htaccess. I solved the issue for the first link, however I can't do it for the second one.
I would like to have for the second link:
www.example.com/profile/name or
www.example.com/profile/id
This is my .htaccess file:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]
# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
The code I submitted above solved my problem.
You can use:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{QUERY_STRING} page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{QUERY_STRING} page=([^&]+)
RewriteRule ^ /%1? [L,R=301]
And for rewrites back
# rewrites back
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

Htaccess redirect with many rules

I got rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301] //add www before adress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] //remove .php
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d //remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^portfolio/([0-9]+)([^.]*)$ /portfolio-single.php?id=$1 [L]
But the last one dosent work, i want to have adress like www.mysite.com/portfolio/3-some-name => /portfolio-single.php?id=3
I dont know how to get i work.
(Sorry for my bad english)
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^portfolio/([0-9]+) /portfolio-single.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Resources