.htaccess friendly url´s issue - apache

I am implementing friendly URLs on my website.
At the moment I have some rules working but still need help to implement the last ones.
Rules working:
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
Rule Working but not as I wanted:
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
Rule not working:
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [L]
About the 3rd rule, I wanted the friendly URL catalogo/homem and tried to change the - for the / but didn't work.
The 4th rule don´t work at all, with - or the /.
Could be a problem of the server's version? This is the version of the server "Apache ver.2.2.19 (Unix)".
My .htaccess file
RewriteBase /
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ /about.php [L]
RewriteRule ^Contactos$ /contact.php [L]
RewriteRule ^catalogo-([a-zA-Z0-9]+)$ catalogo.php?cat=$1
RewriteRule
^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]

I have reformatted your file to the following:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^SobreNos$ about.php [L]
RewriteRule ^Contactos$ contact.php [L]
RewriteRule ^catalogo/([a-z0-9]+)/?$ catalogo.php?cat=$1 [NC,L]
RewriteRule ^products/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ product.php?id=$1&ref=$2&cat=$3 [NC,L]
Here's what I've changed:
Moved your RewriteBase down below RewriteEngin on
Removed the leading slashes in the destinations for each rule (they are not needed because RewriteBase is set)
Removed A-Z from your expressions (because the NC, no case, flag is set)
Used the slash in the 3rd and 4th rules, and appended an optional ending slash.
My test show that everything works properly. The only reason a 404 would be thrown is if the file you are rewriting to (product.php for example) does not exist.

I have the solution for the problem.
Options -Indexes
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ catalogo.php?cat=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/]+)$ catalogo.php?cat=$1&scat=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ product.php?cat=$1&scat=$2&name=$3&id=$4 [L,QSA]
Still have to add a rewrite rule for about.php and contactos.php, but not a huge problem.

Related

MOD_REWRITE - URL ENDING TRAILING SLASH

Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
ServerSignature Off
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]`
Hello I'm struggling really hard with URL closing trailing slash.
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]
I would like, for example, to force a redirection from www.website.com/cookie-policy to www.website.com/cookie-policy/
Use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
It will add a / to end of your URLs. Make sure you clear your cache before testing this.

Rewrite all to https with htaccess and Magento

We want to redirect every page in Magento to https. We know how to change this in Magento, but we do not know how to do this in our current htaccess? For more information, please see our current htaccess file below, stripped from unnecessary comments.
Current htaccess:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
DirectoryIndex index.php
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{THE_REQUEST} \s+/index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1? [L,R=301,NC,NE]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteRule ^home/?$ /? [R=301,L,NC]
# ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH
RewriteRule ^google[0-9a-f]+.html$ - [L]
RewriteRule (.+)\.html$ /$1/ [L,R=301]
RewriteRule (.+)\.html\/$ /$1/ [L,R=301]
# ADD TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# TRAILING SLASH CHECK
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#CHECK IF REDIRECT POINTS TO A VALID FILE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#REWRITE EVERYTHING ELSE TO INDEX.PHP
RewriteRule .* index.php [L]
Solved by adding:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Add the below code
RewriteEngine on
after
RewriteCond %{HTTPS} on
RewriteRule ^$ https://%{HTTP_HOST} [L,R]
And goto admin>SYStem>configuration>General>Web>
change all secure url and unsecure to https:
ex:

apache rewrite rule: http to www doesn't work

I tried to define a apache rewrite-rule to redirect users which type http://mydomain.net to get on my website to www.mydomain.net. So in the .htaccess-file I wrote the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
But this doesn't work: when I type mydomain.net I get in my browser
Forbidden
You don't have permission to access / on this server.
When I try the opposite
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [R=301,L]
that works (when I type in www.mydomain.net I get to mydomain.net). Does anybody know why and how to solve the problem?
Here is my .htaccess:
RewriteEngine On
# www to http
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteRule ^uploads/.*$ - [L]
RewriteRule ^fileadmin/.*$ - [L]
RewriteRule ^typo3conf/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# naechste Zeile auskommentieren,
# wenn .html Endung in RealURL eingestellt ist
RewriteRule (.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule / %{REQUEST_URI}/index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.htm -f
RewriteRule / %{REQUEST_URI}/index.htm [L]
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule / %{REQUEST_URI}/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
You should try it this way. You need a forward slash before $1. However I would do it this way so that it won't affect subdomains. Change the condition to check for the TLD instead of the absence of www.
RewriteCond %{HTTP_HOST} ^mydomain.net [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use the above code it should work and to fix the forbidden error bit you will need to add in the following bit of code in apache httpd.conf file :-
<Directory />
Require all granted
</Directory>
You can find more permission about the 403 error on an article I have written here : - http://codenathan.com/apache/you-dont-have-permission-to-access-the-requested-directory

htaccess issue in accessing the directory

My Blog link is http://www.example.com/blog when i am trying to access it it converts the url into http://www.example.com/blog/?ID=blog which is not correct. But when i put the backslash at the end of the blog it works fine but i don't want this back slash at the end
I try to change every thing inside htaccess file but failed.
Here is my htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^products products.php
RewriteRule ^websitemap sitemap.php
RewriteRule ^aboutus aboutus.php
RewriteRule ^clients clients.php
RewriteRule ^shoppingcart inq.php
RewriteRule ^contactus contact.php
RewriteRule ^finished finished.php
RewriteRule ^product/([a-zA-Z0-9_-]+)$ product_large.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ products.php?ID=$2&MID=$1
ErrorDocument 404 http://www.example.co.uk/error404.php
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk\/" [R=301,L]
Insert this rule after RewriteRule ^ - [L] to force a trailing slash:
DirectorySlash On
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^blog(/.*)?$ - [L,NC]
# your rest of the rules

htaccess rewrite url redirect php file to folder

I want to redirect this php url to the folder.
I always used the settings below. But since the update of apache to 2.4 it doesnt work anymore.
Goal:
example.com/about.php?sub=about or example.com/about.php?sub= should be seen in the browser as example.com/about/
I use the following settings.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(example.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
RewriteRule ^about/$ about.php$1 [L]
RewriteRule ^about/([A-Za-z0-9-]+)$ about.php?sub=$1 [L]
This is what I am using now...
I don't see any rule in your code that is making /about.php?sub= to /about.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php(\?sub=[^\s&]*)?\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^about/?$ about.php?sub=about [L,NC,QSA]
RewriteRule ^about/([a-z0-9-]+)/?$ about.php?sub=$1 [L,NC,QSA]
# Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]