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]
Related
I have tried some different things but can't get this working.
What my code should do:
remove www
use https
if the url doesn't exist go to index.php
1&2 work for me
3 works for me
But when I add 1,2 & 3 together I get an error.
My code is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /index.php [L]
Thank you :)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* https://%1/$0 [R=301,END]
RewriteCond %{HTTPS} =off
RewriteRule .* https://%{HTTP_HOST}/$0 [R=301,END]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ index.php [NS,END]
Using a base is a good idea. Extra groups are not needed in PCRE. Only redirect non-empty URLs as you likely have index.php in your DirectoryIndex already.
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]
i cannot redirect my domain ip to the domain name, i have the following in my htaccess file which im told should work
RewriteCond %{HTTP_HOST} ^37\.61\.233\.81
RewriteRule (.*) http://jpcreativevision.co.uk/$1 [R=301,L]
But thats not working, anyone got any idea why it isnt working? thanks for the help.
Edit
Full htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{HTTP_HOST} ^37\.61\.233\.81
RewriteRule (.*) http://jpcreativevision.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^jpcreativevision\.co.uk
RewriteRule (.*) http://jpcreativevision.co.uk/$1 [R=301,L]
ErrorDocument 404 http://jpcreativevision.co.uk/404.php
Just tested it via http://htaccess.madewithlove.be/ and it says, that your rule should work as expected.
However, you may try a different order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^37\.61\.233\.81
RewriteRule (.*) http://jpcreativevision.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^jpcreativevision\.co.uk
RewriteRule (.*) http://jpcreativevision.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
ErrorDocument 404 http://jpcreativevision.co.uk/404.php
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]
I have a site I've written in php.
The addresses are as follows:
http://www.site.com/page.php
I'd like any request to:
www.site.com/page.php
or
www.site.com/page
To go to:
www.site.com/page/
(And force a www.)
Can anyone help me?
For domain :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
For your first directory :
RewriteRule ^/([^/]*)(\.php)? /$1/
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
Maybe this will work.
This should do what you wanted:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Tim's answer is the closest, but it also does adds the trailing slash to images and css...
So taking Tim's answer, and slightly editing it gives us:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Which should work!