301 to extensionless page only if it exists - .htaccess

If a user goes to:
www.example.com/mypage.asp
I want it to redirect to an extensionless version of the page:
www.example.com/mypage
But I only want to do it if the requested page exists, so:
www.example.com/mypage.asp -> 301 to -> www.example.com/mypage
But www.example.com/notapage.asp -> 404
How best to do this in .htaccess?

You may try this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/+(.+?)\.asp[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f will make sure that it is done for a an existing file only.

Related

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]

URL rewrite to remove trailing folder name

We have a server, and have several folders under the /var/www/ folder.
We have pointed our domain name to the IP of the server, and by default we expect to load one folder as the website (since its not possible to point a folder with DNS).
I have written .htaccess in such a way that when you enter the IP or the domain name, the request redirects to the website folder.
However, whenever we enter the IP or the domain name, the name of the folder is getting added to the URL.
Here is the present .htaccess:
Options +FollowSymlinks -Multiviews
#DirectoryIndex folder/
RewriteEngine on
ReWriteBase /
RewriteRule ^$ /folder [L]
RewriteRule ^$ folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
where the folder is the folder's website
so,
www.domain.com
becomes
www.domain.com/folder/
Is there a way to rewrite the URL to remove the folder name?
Thanks in advance :)
EDIT : Added .htaccess code
Have your rule like this in DocumentRoot/.htacess:
DirectorySlash On
Options +FollowSymlinks -MultiViews
RewriteEngine on
ReWriteBase /
# redirect /folder/abc123 to /abc123
RewriteCond %{THE_REQUEST} \s/+folder/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# skip rewrites for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !^(index\.)?$ - [L]
# route everything internally to /folder
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
It sounds like you made an external redirect instead of an internal rewrite. An external redirect is denoted by the [R] flag (with optional parameter) and causes Apache to send a 301 or 302 header back to the client with a different url that client should request. This causes the client to show the new url in the address bar.
What you want is an internal rewrite. When you request the url, the url is internally rewritten to the place where the resource is actually located. You do this by omitting the [R] flag, and not using a domain name in the rewritten part. It typically looks something like this:
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ /folder/$1 [L]

htaccess rewrite doesn't work

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).

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

How do i redirect a directory to the parent directory in htacess

I have this website http://www.foo.com/bar/index.php. I recently moved my site FROM foo.com/bar/index.php TO foo.com/index.php
In my htaccess file, how do i write the conditions so that if any request is made from lets say .foo.com/bar/somedirectory/somefile REDIRECT to foo.com/somedirectory/somefile
In the htaccess file in your document root, preferably above any rules that do routing (to index.php, for example), add these rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/bar/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^/?bar/(.*)$ /$1 [L]
If you want to externally redirect the client (changing the URL in the browser's address bar), then add the R flag in the rule's brackets: [L,R=301].
Add below to your foo.com/bar/.htaccess file:
RewriteEngine On
RewriteRule ^(.*)?$ http://%{HTTP_HOST}/$1 [R=301,QSA,L]

Resources