htaccess rewrite doesn't work - .htaccess

I'm new with htaccess and with a little help I could write this, but I'm having some problems and I don't know how to solve them
# General setup
RewriteBase /
RewriteEngine on
Options -Indexes
CheckSpelling off
# rewrite php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]
# rewrite html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.html
RewriteRule ^ /%1 [L,R=301]
# error pages
ErrorDocument 404 error
Problems:
I'd like to transform somefile?var=value to somefile/value and it doesn't work
If I write somefile/anything I get somefile without css style instead of being redirect to the error page
As I'm new with htacess I don't know if that file is secure enough
What I'd like:
Redirect any extension to a filename file (with no extension)
Protect my htaccess and directory and files
redirect any users?id=id_user to users/username
Do you know any way to construct the htaccess online? or Do you have any idea or advice to give me?
Thank you!!!!!!

You're not getting CSS probably because your links are relative URLs, and when you have multiple paths in your URL, the relative links all break. You can try adding this to your page headers:
<base href="/" />
The somefile/anything is because the -f together with `%{REQUEST_FILENAME} actually checks for path info requests, so you want to use this instead:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
and
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
For values, try adding this before your other rules:
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1/%2?%3 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/([^/]+)$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.+)/([^/]+)$ /$1.php?var=$2 [L,QSA]
For users, you're not going to be able to use htaccess because the URL /users?id=userid does not contain the username. Mod_rewrite has no idea what user ID maps to what user name. You'll need to do this using a php script (like users.php).

Related

Removing .php extension except sub directory called enquire

Please assist me to stop this from removing the ".php" extension in sub directories eg: https://www.example.com/enquire/contactmail instead of: https://www.example.com/enquire/contactmail.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
\#####exclude /cp folder####
RewriteCond %{REQUEST_URI} !^/enquire
RewriteCond %{REQUEST_FILENAME}\\.php -f
RewriteRule ^(.\*)$ $1.php
RewriteCond %{THE_REQUEST} ^\[A-Z\]{3,9}\\ /((?!cp)\[^.\]+)\\.php
RewriteRule ^/?(.\*)\\.php$ /$1 \[NC,L,QSA,R=301\]
RewriteRule ^enquire/(.\*)$ contactmail.php?s=$1 \[NC,L,QSA\]
RewriteRule ^enquire/(\[0-9\]+)$ contactmail.php?a=$1 \[NC,L,QSA\]
As I understand you have a working solution in place to rewrite incoming requests without ".php" extension to internal resources with that "file name extension".
And now you ask how you can add an exception from that, so that the internal rewrite does not get applied to requests, that target resources inside certain folders in the requested path, physical or virtual folders.
If that is correct then indeed you should implement an exception for those requests. Exceptions should get implemented before more general rules. So further up in the configuration file since that is processed from top to bottom.
That should roughly be what you are looking for:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
# exception for the /enquire request path
RewriteRule ^enquire/(\d+)$ contactmail.php?a=$1 [NC,END,QSA]
RewriteRule ^enquire/(.*)$ contactmail.php?s=$1 [NC,END,QSA]
# redirect requests that specify a ".php" extension
RewriteRule ^(.*)\.php$ $1 [NC,L,QSA,R=301]
# rewrite requests if a corresponding php file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
Just Use the blow .htaccess to remove the .php extension
IndexIgnore * # prevent directory listing
Order deny,allow
Allow from *
# ------------------------------------------
# Rewrite so that php extentions are not shown
RewriteEngine on
#.php URL Rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

htaccess redirecting to the wrong page when using multiple rewriterules

I have a htaccess file which looks like the following:
Options -Indexes
RewriteEngine On
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# only allow rewriting to paths that dont exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# example.com/username
RewriteRule ^([\w\d_\-]+)/?$ profile.php?uid=$1 [L,QSA]
# example.com/username/$tab
RewriteRule ^([\w\d_\-]+)\/([\w]+)/?$ profile.php?uid=$1&tab=$2
# example.com/listen/$id
RewriteRule ^listen\/([\w\d_\-]+)?\/?([\w\d_\-]+)?$ track.php?id=$1&secret=$2 [L,QSA]
The way it is supposed to work is to redirect any URL looking like these:
1. example.com/example points to profile.php?id=example
or
2. example.com/example/tab points to profile.php?id=example&tab=tab
3. example.com/listen/example points to track.php?id=example
or
4. example.com/listen/example/code points to track.php?id=example&secret=code
The problem is that sometimes, a link which looks like the third one will point to the profile page. However, what's weirder, is that if example has a dash in it, it will point to the right place. This shouldn't be happening because my regex is matching after listen.
All help is appreciated.
Both of your listen/ rules should appear before other rules and moreover 2 RewriteCond are only being applied to next immediate RewriteRule.
You may use these rules to replace all of your code:
Options -Indexes
RewriteEngine On
# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# example.com/listen/$id
RewriteRule ^listen/([\w-]+)/?$ track.php?id=$1 [L,QSA,NC]
# example.com/listen/$id/secret
RewriteRule ^listen/([\w-]+)/([\w-]+)/?$ track.php?id=$1&secret=$2 [L,QSA,NC]
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# example.com/username
RewriteRule ^([\w-]+)/?$ profile.php?uid=$1 [L,QSA]
# example.com/username/$tab
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?uid=$1&tab=$2 [L,QSA]
Also note that \w means [a-zA-Z0-9_] so no need to add \d_ in character class.

How can I rewrite a 'subfolder' to a script?

I need to redirect a URL example.com/servers/1/ to a PHP script (example.com/serverinfo.php) that recieves the 1 part of the requested URL as a GET variable (or similar).
I currently have a rule RewriteRule ^([^/.]+)$ $1.php that rewrites URLs such as example.com/servers to example.com/[file].php, and tried to base the following rule off that:
RewriteRule ^/?servers/([0-9]+)$/ /serverinfo.php$
The new rule has no effect (other than causing the page's CSS to fail to load), what am I doing wrong?
You can use:
Options -MultiViews
RewriteEngine On
# handle /servers/123
RewriteRule ^/?servers/([0-9]+)/?$ /serverinfo.php?num=$1 [L,QSA,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Redirect removing (.htm) extension without using RewriteRule for a single page

I tried this simple .htaccess redirect to remove the (.htm) extension from a single page.
Redirect 301 /mypage.htm http://www.mydomain.co.uk/mypage
However it returned a 404 page not found?
Dont use redirect, use mod-rewrite to rewrite your url.
Try :
RewriteEngine on
#1)redirect "/mypage.htm" to "/mypage"
RewriteCond %{THE_REQUEST} /mypage\.htm [NC]
RewriteRule ^ /mypage [NC,L,R]
#2)dont rewrite existing dirs and files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#3)rewrite "/mypage" to "/mypage.php"
RewriteRule ^mypage/?$ /mypage.htm [NC,L]

htacces redirect for SEO friendly URLs

I have my htaccess file setup, so that the pages remove extensions. Now, I am trying to make the pages that transfer variables, into SEO friendly urls ... so, for example...
http://www.example.com/art-gallery?page=2 ... which is actually "art-gallery.php?page=2", would turn into... http://www.example.com/art-gallery/page/2
Or... http://www.example.com/art-piece?id=3 ...would go to... http://www.example.com/art-piece/id/3
... and so on ...
I have alot in my htaccess file, and am not sure how to do the above (there are plenty of tutorials on going from www.example.com/index.php?page=2 to www.example.com/page/2/ but none that do exactly what I need). Ideally, I'd like to be able to do this for all similar pages...
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# Force www:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# Remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# Remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# Remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# Forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Disable Directory Browsing
Options -Indexes
# Disable Hotlinking of Images
# with forbidden or custom image option
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# Protect htaccess File
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
You can transfer parameters with the variable QUERY_STRING.
Consider the following rule:
RewriteRule ^index.html index.php?%{QUERY_STRING}&m=main&a=index
This rule would transform
index.html?something=value
into
index.php?something=value&m=main&a=index
You should use the RewriteEngine.
You could also use a 301 redirect either alone or in conjunction with the RewriteEngine to redirect SEs.
Generally, though redirecting SEs to a different page than what users will see is not a good practice, and may result in your pagerank decreasing. Instead, try migrating all your pages to the second URL format, and consider using 301 redirects to help the transition.
Generally: Use 301 redirects for SE-friendly page changes. See this SO for additional reference.
You can insert this rule just before Forward request to html file rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/])/([^/])/([^/])/?$ $1.php?$2=$3 [L,QSA]
This is quite old but why not do the following:
RewriteEngine On
RewriteRule ^([^?]*) index.php?route=$1 [L,QSA]
Then in your index.php you can handle it like such;
if (isset($_GET['route'])) {
$route = explode('/', $_GET['route']);
if (iconv_strlen((end($parts)), 'UTF-8') == 0) {
array_pop($parts);
}
}
From here your main level would be handled with $route[0], second level $route[1]
For example;
http://example.com/art-gallery/2
$route[0] would equal 'art-gallery'
$route[1] would equal '2'

Resources