Mod_rewrite redirect issue - .htaccess

I have this .htaccess file:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie$ galerie.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie/$ galerie.php?user=$1 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)/$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*).php?$ index.php?request=$1 [QSA,L]
and it doesnt seem to work.
Here is my problem: when I want to access a page on my site e.g. site.com/test.php,
it redirects to index.php?request=test. The problem is that I also have "profil.php" and "galerie.php" that I dont want to pass through index.php. Instead when I do this: site.com/username/profil, I want it to point to profil.php?user=username, but it doesnt work.
Any suggestions?

i've put something like this:
RewriteCond %{REQUEST_URI} !^(.*)\.php?$
RewriteRule ^(.*)\.php?$ index.php?request=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/personal$ personal.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/personal/$ personal.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie$ galerie.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/galerie/$ galerie.php?user=$1 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
RewriteRule ^(.*)/galerie-(.*)_(.*)/$ galerie_poze.php?user=$1&album=$2&page=$3 [NC]
and when i do /username or /username/profil it works BUT it no longer works with the first rule id i do sitename.tld/test.php it doesnt work....

You want one of your two first rules to match [username]/profil (where it now matches [username] and [username]/)
Try
RewriteRule ^([a-zA-Z0-9_-]+)/profil$ profil.php?user=$1 [NC, L]

Use conditionals.
Make sure the file or directory doesn't exist:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You can also restrict redirects by using something like:
RewriteCond %{REQUEST_URI} !^/profil\.php$ [NC]
Make sure to properly escape.
This needs to be escaped:
RewriteRule ^(.*).php?$ index.php?request=$1 [QSA,L]
Like:
RewriteRule ^(.*)\.php?$ index.php?request=$1 [QSA,L]
Also,
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1 [NC]
Can be simplified to:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profil.php?user=$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 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]

Weird behavior with multiple conditions

I have the following rules:
RewriteCond %{REQUEST_URI} !script.php
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule .* script.php?q=%1
RewriteCond %{REQUEST_URI} !script.php
RewriteRule (.*) script.php?q=$1
What I'm trying to accomplish here is that every access to http://example.com/?url=www.google.com is redirected to script.php?q=www.google.com and if there is no query string, relative path should be sent to script.php, for example in http://example.com/path, should be redirected to script.php?q=path. This doesn't seem to work with my rules, but I'm not sure why. Is this even possible ?
You can have your rules as this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([^&]+) [NC]
RewriteRule ^/?$ script.php?q=%1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)$ script.php?q=$1 [L,QSA]

htaccess 301 redirect everything except /admin

I have been scratching my head for almost half an hour already because of this. I don't know what I am doing wrong, might just be under my nose, but i just cant see it. Here's what I have on my .htaccess
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
When I access example.com/admin, i am still redirected to newsite.com/admin.
Can anyone please advise? Thanks!
UPDATE: This is my everything that I have on mod_rewrite currently:
RewriteRule ^googleabcdef12345.html - [L]
RewriteCond %{HTTP_HOST} ^sample1\.no$ [NC]
RewriteRule ^(.*)$ http://www.newsample1.no/$1 [L,R=301]
RewriteCond %{HTTP_HOST} example\.com$ [NC]
#RewriteRule admin - [S=1]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It is a drupal website btw. I tried QUERY_STRING instead of REQUEST_URI but no luck still.

.htaccess adding language support for shop

I'm trying to create multilingual shop having RewriteRules as follows:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]
RewriteRule .*[^/]$ $0/ [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\$
RewriteRule ^(.*)$ $1?language=en [QSA]
RewriteRule ^catalog/product/([^/]*)/$ catalog/index.php?id=$1 [L,QSA]
RewriteRule ^catalog/([^/]*)/$ catalog/index.php?category=$1 [L,QSA]
Those rules don't work when trying to access http://example.com/catalog/product/111/
or http://example.com/catalog/category/
However when I delete
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\$
RewriteRule ^(.*)$ $1?language=en [QSA]
everything works just fine.
What is the problem? How can I make this stuff work together?
You need to switch the order of the rules, and get rid of the L flag for some of them. This should work:
RewriteRule ^catalog/product/([^/]*)/ catalog/index.php?id=$1 [QSA,S=1]
RewriteRule ^catalog/([^/]*)/ catalog/index.php?category=$1 [QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)$ $1?language=en [QSA]

Resources