Use Two rules for Htaccess - php

This line is working
RewriteRule ^(.*)/(.*)?$ site/$1/pagina.php?pagina=$2 [L]
Now I'm trying to make when accessing the link /inicio go to the index.php page.
RewriteRule ^inicio/(.*)?$ site/$1/index.php [L]
Thereby
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^site/
RewriteRule ^inicio/(.*)?$ site/$1/index.php [NC,L]
RewriteRule ^(.*)/(.*)?$ site/$1/pagina.php?pagina=$2 [L]
.
.
.
.
Current code, with the help of friend #anubhava
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/site/ [NC]
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/inicio?$ site/$1/index.php [NC,L,QSA]
RewriteRule ^([^/]+)/noticia/([^/]+)/?$ site/$1/pagina.php?pagina=pag_noticiasVer.php&id=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ site/$1/pagina.php?pagina=$2 [L,QSA]

Have it this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/site/ [NC]
RewriteRule ^ - [L]
RewriteRule ^inicio/([^/]+)/?$ site/$1/index.php [NC,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ site/$1/pagina.php?pagina=$2 [L,QSA]
RewriteCond are only applicable for next RewriteRule.

Related

rewriteRule .htaccess not working properly

I'm trying to do a RewriteRule in .htacess but it's not working and I don't know why.
This is my code
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1/$2 [R=301,L]
RewriteRule ^agencia/agencia-marketing-digital-bh$ /agencia/section.php?id=$1 [L]
How it should work:
User acess www.mysite.com/agencia-marketing-digital-bh.php and it should change to www.mysite.com/agencia/agencia-marketing-digital-bh.php
Can someone please tell me what I'm doing wrong?

How to remove html and php extensions from URL

Hello I'm trying to hide html and php extension from my site URLs and here's my .htaccess file
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/install -d
RewriteCond %{REQUEST_URI} !(install) [NC]
RewriteRule ^(.*) /install/ [L,redirect=302]
RewriteCond %{REQUEST_URI} ^(.+)\!$
RewriteRule ^(.*) stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~s$
RewriteRule ^(.*) stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~q$
RewriteRule ^(.*) generate_qr.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~p$
RewriteRule ^(.*) preview_url.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)\.html$
RewriteCond %{REQUEST_URI} !^(.+)\.htm$
RewriteCond %{REQUEST_URI} !^(.+)\.php$
RewriteCond %{REQUEST_URI} !^(.+)\.js$
RewriteCond %{REQUEST_URI} !^(.+)\.css$
RewriteCond %{REQUEST_URI} !^(.+)\.jpg$
RewriteCond %{REQUEST_URI} !^(.+)\.png$
RewriteCond %{REQUEST_URI} !^(.+)\.gif$
RewriteCond %{REQUEST_URI} !^(.+)\.swf$
RewriteCond %{REQUEST_URI} !^(.+)\.xml$
RewriteCond %{REQUEST_URI} !^(.+)\.ico$
RewriteCond %{REQUEST_URI} !^(.+)\.txt$
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !^(.+)/$
RewriteRule ^(.*) url_redirector.php?url=$1 [L]
RewriteRule ^(.*).html$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Any ideas how can i do that? I've tried a lot of answers that I found but it's not working out for me.
You can refactor your rules to reduce lot of redundancy and have 2 additional rules for hiding .php and .html extensions:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/install -d
RewriteRule !^install/ /install/ [L,NC,R=302]
RewriteRule ^(.+(?:!|\~s))$ stats.php?url=$1 [L,QSA]
RewriteRule ^(.+\~[pq])$ preview_url.php?url=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_URI} !\.(php|js|css|jpe?g|png|gif|swf|ico|xml|txt|html?)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* url_redirector.php?url=$0 [L,QSA]
Removing Extensions
To remove the .php extension from a PHP file you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension from a html file
RewriteRule ^([^\.]+)$ $1.html [NC,L]
To remove .html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Updated
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

.htaccess redirect loop error

I created a .htaccess with the following goals:
1: Remove .php and force add trailing slash
2: Make http://website.com/sticker/fruit/apple = sticker.php?category=fruit&alt=apple
That works, but whenever I type something that does not exist, for example:
http://website.com/sdgsdg/ OR
http://website.com/dssdg/sdgsg/zkzzv/
I get redirect errors.
Here is my .htacess:
Options +FollowSymlinks
RewriteEngine on
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php/$ http://website.com/$1/ [L,R=301]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ http://website.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://website.com/$1/ [L,R=301]
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]
Can anyone spot whats causing the redirect loops?
Thank you in advance.
I found a solution that worked for me.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^sticker/([^/.]+)/([^/.]+)/$ sticker.php?category=$1&alt=$2 [NC,L]

Can't goto root of website anymore

I'm making a new website and had lots of help overhere before with the htaccess to make it all work. Nevertheless I can't reach the root of the website www.placewomen.com anymore...it's looking for /c/c when I aspect the index.html of the root....My htaccess looks like this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^placewomen.com [NC]
RewriteRule ^(.*)$ http://www.placewomen.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+new/index\.php\?var=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -l
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?var=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/placeholder.jpg -f
RewriteRule ^(.*)$ /$1/placeholder.jpg [L]
Try this code:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^placewomen\.com$ [NC]
RewriteRule ^(.*)$ http://www.placewomen.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+new/index\.php\?var=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/placeholder.jpg -f
RewriteRule ^(.*)$ /$1/placeholder.jpg [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/c/$1 -l
RewriteRule ^(.+)$ /c/$1 [L]
RewriteRule ^(.+)$ /new/index.php?var=$1 [QSA,L]

.htaccess doesn't rewrite the URL as it should

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^search/([^/]+)/?$ search.php?q=$1
RewriteRule ^user/([^/]+) user.php?proc=$1
When I go to: http://localhost/user/register/, and try to echo the variable $_GET["proc"], I get: register.php instead of register. Where is my mistake?
Ordering of your rules seems to be a problem here.
Keep your .htaccess like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search.php?q=$1 [L,QSA]
RewriteRule ^user/([^/]+) /user.php?proc=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ /$1.php
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]

Categories