I'm using the below htaccess file.First one is working fine.But the second one is not working.
RewriteEngine On
RewriteBase /Epropertiesnew/
RewriteCond %{THE_REQUEST} /Properties(?:\.php)?\?project=([^&]+)&property=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /ViewNewProjects(?:\.php)?\?newproject=([^&]+)&url=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ Properties.php?project=$1&property=$2 [NC,L,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewNewProjects.php?newproject=$1&url=$2 [NC,L,QSA]
If I commented the first one,second one is working fine.Please help me to fix this.
Related
I have a site with a big .htaccess with a lot of dynamic rules, everything works fine, but unfortunately, Google is duplicating my URLs, considering the same URL with trailing slash and without... I will paste the code of my .htaccess if someone could help me to enforce adding the trailing slash, without generating a 301 loop.
#Options -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://www.advogadosaqui.com.br/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## Adding a trailing slash <<<< (HERE IS WHATS I TRIED) >>>>
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_locker/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_locker/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_estado/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_estado/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_cidade/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_cidade/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_bairro/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_bairro/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
# To externally redirect /dir/file.php to /dir/file
# %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
#RewriteRule ^ /%1 [R=301,NE,L]
#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# internal rewrite from root to /artigos/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# internal rewrite from root to /unidades/pagina_agencia/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
# internal rewrite from root to /unidades/pagina_locker/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
# internal rewrite from root to /unidades/pagina_estado/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
# internal rewrite from root to /unidades/pagina_cidade/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
# internal rewrite from root to /unidades/pagina_bairro/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
#RewriteRule ^(.+?)/?$ $1.php [L]
# remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##ErrorDocument 404 https://www.advogadosaqui.com.br/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
I tried to add this rule, but it's generating a 301 loop:
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
UPDATED Question
After using Anubhava's rules everything works fine without any 301 loopings, the problem is when the request comes without the trailing slash... because now it's generating one unnecessary 301 to enforce the trailing slash, I need to find a way to do it with only one redirect...
Some prints to explain what's going on:
###FIRST JUMP - THE REQUESTED URL
###SECOND JUMP
###LAST JUMP
Instead of this, I need to do one redirect directly to the URL with: https + www + trailing slash
FINAL UPDATE
Ok I found a solution... using page rules in Cloudflare / removing the enforce https and www from the local file (.htaccess) and changing the rules to add a trailing slash to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
The final .htaccess looked like this:
Options -MultiViews
RewriteEngine On
# Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# internal rewrite from root to /artigos/
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# handle .php extension internally
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
By doing this I managed to avoid the extra redirect ;)
You have a massive .htaccess. I will try to combine and merge few rules to shorten the length and also handle adding slash removal:
Options -MultiViews
RewriteEngine On
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ https://www.avantitecnologiati.com.br%{REQUEST_URI}/ [L,R=301,NE]
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+(?:artigos|unidades/pagina_(?:agencia|locker|estado|cidade|bairro))/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# Remove .php extension externally
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# internal rewrite from root to /artigos/
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to use a different browser or remove cache data from your browser to test this rule to avoid old cache.
With your shown samples, attempts please try following .htaccess rules file. Apart from fix of trailing slashes I have clubbed 4 rules into 1 Rule.
Make sure to clear your browser cache before testing your URLs.
#Options -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)/?$ http://www.avantitecnologiati.com.br/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)/?$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## Adding a trailing slash.....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ $1/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_locker/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_(?:locker|estado|cidade|bairro)/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
# To externally redirect /dir/file.php to /dir/file
# %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
#RewriteRule ^ /%1 [R=301,NE,L]
#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# internal rewrite from root to /artigos/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# internal rewrite from root to /unidades/pagina_agencia/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
# internal rewrite from root to /unidades/pagina_locker/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
# internal rewrite from root to /unidades/pagina_estado/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
# internal rewrite from root to /unidades/pagina_cidade/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
# internal rewrite from root to /unidades/pagina_bairro/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
#RewriteRule ^(.+?)/?$ $1.php [L]
# remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##ErrorDocument 404 http://www.avantitecnologiati.com.br/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
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]
my htaccess codes add a slash to the end of all URLs but I just want it to add slashes to files ending with the .php file extension how can I do that?
my htaccess codes :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Could you please try following. Written with shown samples only. Please make sure to clear browser cache before testing your URLs
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/seo$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^ /serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
So in the first part of the code I just turn http into https. My aim is that I can use urls without an .php but in my old code I had the problem that if I did so, it was used as an id for my serve.php page. So if use https://example.com/contact it was like https://example.com/serve.php?id=contact but I want it to work as https://example.com/contact.php but on the other side I want that ids that arent directions or file should still work as ids. My old code was...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]
I need some help to get friendly URL's on my website.
These are my links i need to rewrite:
www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number
This is my .htaccess. I solved the issue for the first link, however I can't do it for the second one.
I would like to have for the second link:
www.example.com/profile/name or
www.example.com/profile/id
This is my .htaccess file:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]
# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
The code I submitted above solved my problem.
You can use:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{QUERY_STRING} page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{QUERY_STRING} page=([^&]+)
RewriteRule ^ /%1? [L,R=301]
And for rewrites back
# rewrites back
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]