Htaccess same rule for 2 files not working - .htaccess

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]

Related

.htaccess replace url to title without .html extension

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]

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]

Matching a question mark in .htaccess

I'm trying to set up some 301 redirects for old Kayako helpdesk URL's. The old URL's were;
example.com/index.php?/rest_of_url
I've set up rewriting so the URL's are prettier example.com/rest_of_url, and I'm trying to 301 redirect all old URL's to new. I've tried the following but they don't appear to work;
RewriteRule ^index.php?/(.*)?$ /$1 [R=301,L]
RewriteRule ^index.php\?/(.*)?$ /$1 [R=301,L]
RewriteRule ^index.(.*)/(.*)?$ /$2 [R=301,L]
I can't quite figure it out.
EDIT:
Here is the whole .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^/(.*)$
RewriteRule ^index\.php$ /%1? [R=302,L]
RewriteCond $1 !^(admin|api|console|favicon\.ico|robots\.txt|sitemap\.xml|index\.php|tiny_mce_gzip\.php|cron|onsite|staff|rss|setup|visitor|winapp|wallboard|__swift) [NC]
RewriteCond $1 !\.(jpg|jpeg|png|gif|js|css|htm|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(favicon\.ico|robots\.txt|sitemap\.xml|index\.php|tiny_mce_gzip\.php|__swift) [NC]
RewriteCond $1 !\.(jpg|jpeg|png|gif|js|css|htm|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]*)/(.*)$ $1/index.php?/$2 [L]
</IfModule>
The part after the question mark is not in the RewriteRule url.
You have to use %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} ^/(.*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
%1is the first (.*) in the last RewriteCond.
In your case, without loop, use:
RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

htaccess redirect rule throws a 500 error

I have the following content in my htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+).html$ http://www.somedomain.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\?s=([^\s]*)
RewriteRule ^/?(.*?)/?$ %1?%2%3 [L,R=301]
This is supposed to convert queries into user friendly URLs. I had used this same htaccess on other servers before and it always worked but on some server I get a 500 error. What could be the reason?
Thank you.
You need to reorder your rules:
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)\.html$ /$1 [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+index\.php\?s=([^\s&]+) [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+?)/?$ index.php?s=$1 [L,QSA]

Mod Rewrite file.php? to string

I have old site with links like this:
http://domain.com/image.php?690
And I would like to change it into:
http://domain.com/old690
I have tried many different Rules, fe.:
RewriteRule ^image.php?(.+) old$1 [L]
EDIT: All rules looks like:
RewriteEngine On
RewriteRule ^admin/(.*) ninja-admin/$1 [L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
RewriteRule ^image.php\?(.+) old$1 [L]
What is correct RewriteRule, and why?
You're doing it upside down.
Put this code in your htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The rule for your images is the following
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
It will forward every url like /old123 (where 123 is one or more digits) to image.php?123
EDIT: if you want to forbid direct access to image.php?xxx then you can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/image\.php\?([0-9]+) [NC]
RewriteRule ^ old%1 [R=301,L]
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
I use this so http://example.com/old4444 forwards to http://example.com/image.php?file=4444.
Also, the browser will keep http://example.com/old4444 in the address bar.
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} file=([0-9]+)
RewriteRule ^/?image.php$ %1? [R=301,L]
RewriteRule ^/?old([-a-zA-Z0-9_+]+)$ image.php?marker&file=$1 [L]

Resources