Hi guys i need help with apache htaccess.
I've this 2 url:
test / en / guns2 / beretta/
test / en / 1564 / vibram-beretta-m9a3/
how i can catch in a different way this 2 situations?
actually i've this htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/test/admin[NC]
RewriteCond %{REQUEST_URI} !^/test/(it|en) [NC]
RewriteRule ^(.*)$ /test/en/$1 [R=301,L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$ /test/index.php?detMod=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$ /test/index.php?page=$2&category=$3 [L,QSA,NC]
But i need another rule for the case of number (1564) instead of the (guns2)
Use \d+ to match only numbers and use [\w-]+ to match alphanumerics, _ and -:
^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$
and
^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$
Suggested .htaccess:
RewriteCond %{REQUEST_URI} !^/test/admin[NC]
RewriteCond %{REQUEST_URI} !^/test/(it|en) [NC]
RewriteRule ^(.*)$ /test/en/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$ test/index.php?detMod=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$ test/index.php?page=$2&category=$3 [L,QSA,NC]
Related
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]
I have 3+ websites, example:
www.example.com/requestedpage
www.esempio.it/requestedpage
primer.ru/requestedpage
All websites points to the same server's directory. There are 3 directories in it: "en", "it", "ru" with localized requestedpage in them. I want to internally redirect those requests (only PHP, not images) to folders without changing URL. So .com website read pages from "en" folder it - "it", ru - "ru" folder but user shouldn't see that.
I tried some things but they doesn't work..
RewriteCond ^(.+)\.(ru)$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru/$1 [NC,L]
RewriteCond %{HTTP_HOST} ^(.+)\.(ru)$
RewriteRule ^(.+)\.php$ ru%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} MYWEBSITE.ru$ [NC]
RewriteCond %{HTTP_HOST} !ru/
RewriteRule ^(.*)$ /ru/$1 [NC,L]
After a lot of tests and thanks to anubhava help I found a way to deal with it, not nice way, and I think it should be improved:
# ===== MULTILANGUAGE WEBSITES =====
#Rule for IT website
RewriteCond %{HTTP_HOST} !ITALIANDOMAIN\.it$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ it/$1/$2
RewriteRule ^([^/]+)$ it/$1
RewriteRule ^$ it/ [NC,L]
#Rule for FR website
RewriteCond %{HTTP_HOST} !FRANCEDOMAIN\.com$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ fr/$1/$2
RewriteRule ^([^/]+)$ fr/$1
RewriteRule ^$ fr/ [NC,L]
#Rule for RU website
RewriteCond %{HTTP_HOST} !(RUSSIANDOMAIN1\.com)$
RewriteCond %{HTTP_HOST} !(xn--10-olc1a0ag\.xn--p1ai)$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ ru/$1/$2
RewriteRule ^([^/]+)$ ru/$1
RewriteRule ^$ ru/ [NC,L]
You can use this rule from a .htaccess placed inside the parent folder of of it, en, ru folders:
RewriteEngine On
# skip all files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} \.(ru|it)$
RewriteCond %{REQUEST_URI} !^/(?:ru|it)/ [NC]
RewriteRule ^.*$ %1/$0 [L]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/en/ [NC]
RewriteRule ^.*$ en/$0 [L]
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]
Below works for passing directory names while doing a htaccess rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) /index.php?xa=$1&xb=$2&xc=$3&xd=$4 [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /index.php?xa=$1&xb=$2&xc=$3 [NC]
RewriteRule ^([^/]+)/([^/]+) /index.php?xa=$1&xb=$2 [NC]
However when adding to above, the final line to also catch server.com/whatever situations:
RewriteRule ^([^/]+) /index.php?xa=$1 [NC]
I get a 500 server error...
How come?..
Thanks!
There are 2 problems:
RewriteCond before first rule is being applied to very next RewriteRule only
No anchors $ in your regex for RewriteRule.
Here is the fixed code:
RewriteEngine On
# ignore all rules below from files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2&xc=$3&xd=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2&xc=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?xa=$1&xb=$2 [L,QSA]
I want to rewrite the URL like below:
URL: http://localhost/my_site/?file_name=sample
This is my URL I want to show it like:
URL: http://localhost/my_site/sample
that means I want to remove file_name parameter and get the parameter value and set it in URL, for setting this I have used below code:
RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /?file_name=$1 [QSA,L]
But its not working, when I type http://localhost/my_site/sample its showing me the list of all sites present in my local that means its taking me to http://localhost instead of require page. What wrong I am doing?
Please help, Thanks in advance
Assuming that mysite folder is under DocumentRoot. add this to your
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^/(my_site)/([\w\d-]+)/$ [NC]
RewriteRule ^ %1/?file_name=%2 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^(my_site) $1/%2/?%1%3 [L,R]
For server side:
RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]
try this, if it works:
RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ file_name=/$1 [L]
caution: untested