.htaccess replace url to title without .html extension - .htaccess

I have an htaccess file like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteRule ^index.html index.php [NC]
RewriteRule ^sayfa-(.*).html sayfa.php?url=$1 [NC]
RewriteRule ^urunler.html urunler.php [NC]
RewriteRule ^urun-kategori-(.*).html urun-kategori.php?url=$1 [NC]
RewriteRule ^urun-(.*).html urun.php?url=$1 [NC]
RewriteRule ^foto-galeri.html foto-galeri.php [NC]
RewriteRule ^galeri-(.*).html galeri.php?url=$1 [NC]
RewriteRule ^bloglar.html bloglar.php [NC]
RewriteRule ^blog-kategori-(.*).html blog-kategori.php?url=$1 [NC]
RewriteRule ^(.*).html blog.php?url=$1 [NC]
RewriteRule ^iletisim.html iletisim.php [NC]
RewriteRule ^videolar.html videolar.php [NC]
RewriteRule ^video-(.*).html video.php?url=$1 [NC]
RewriteRule ^insan-kaynaklari.html insan-kaynaklari.php [NC]
RewriteRule ^arama.html arama.php [NC]
As you can see, it converts the blog.php url to title+.html
what i want is it just convert it to title.
To do this, when I change the 16th line in my code, I get a "too many redirects" error.
RewriteRule ^(.*) blog.php?url=$1 [NC]
Full htaccess code: https://pastecode.io/s/2x8spyw8

Have your .htaccess rules file in following manner. We need to combine rules for same pattern then also.
Make sure:
To clear your browser cache before testing your URLs.
keep your htaccess file along side with your php files only.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(index|urunler|foto-galeri|bloglar|iletisim|videolar|insan-kaynaklari|arama)\.html/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(sayfa|urun-kategori|urun-|galeri-|blog-kategori-|video-)/(.*)\.html/?$ $1.php?url=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html/?$ blog.php?url=$1 [NC,QSA,L]

Related

htaccess rule issue in redirecting the url

I want to redirect all below pattern url to before "?" url
i.e
Xyz.com/worksheet/rebus/?random=testing11
Xyz.com/worksheet/rebus/?abc
Xyz.com/worksheet/rebus/?l=1
should be redirected to
Xyz.com/worksheet/rebus/
I tried many things but not able to succeed. How can i do using .htaccess
Rules tried
RewriteBase /worksheet/
RewriteRule ^rebus/?$ /worksheet/rebus/ [L,R=301]
Overall
RewriteEngine On
RewriteBase /worksheet/
RewriteCond %{THE_REQUEST} \s/(rebus)/\?(\S+)\s [NC]
RewriteRule ^ /worksheet/%1/ [R=301,L]
#RewriteRule ^rebus/?$ /worksheet/rebus [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
Have it this way:
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus? [L,R=301,NC]
# skip all rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]

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]

Mask a directory folder name with htaccess

I have this URL: https://example.com/sub/product and I don't want to show "sub" in my URL so that the final URL would look like https://example.com/product and would redirect the indexed URLs to this URL. Is it possible? Here is my current .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule ^([a-z\-0-9]+)$ index.php?goto=$1 [L,NC]
ReWriteRule ^([a-z\-]+)/([a-z\-0-9]+)$ index.php?goto=$1&catidx=$2 [L,NC]
ErrorDocument 404 /404/404.php
You can use the following :
RewriteEngine on
#--redirect from "/sub/foo to "/foo"--#
RewriteCond %{THE_REQUEST} /sub/([^\s]+) [NC]
RewriteRule ^ /%1 [L,R]
#--rewrite "/foo" to its orignal location "/sub/foo"
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /sub/$1 [L,QSA]

Htaccess same rule for 2 files not working

I am trying to create friendly pagination using htaccess file.
But its not working I guess I am using wrong rule for files.
Check out my codes below.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/$ cat.php?id=$1 [NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
Everything is working fine, only cat2.php is not working.
How to fix it?
You need to switch the order around between your 2 cat rules. (.*) matches everything, including slashes, so it will always match whatever cat2 matches. Try just swapping their orders and include L flags:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [L,NC,QSA]
RewriteRule ^(.*)/$ cat.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]

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]

Resources