.htaccess redirect - .htaccess

i want to create a clean url, as in, let say, i have a site www.mywebsite.com, i want, if anybody tries http://www.mywebsite.com/something, he should get redirected to www.mywebsite.com,
so any thing that is typed ahead of mywebsite.com/ they should get redirected to www.mywebsite.com
how can i do this in .htaccess, kindly help.

RewriteEngine ON
RewriteRule ^/.+ / [L,R]
That will redirect everything to / as a permanent redirect. Look at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for more information on the flags you can add for transparency etc etc.
If you want this in .htaccess make sure you have the correct override directives in your <VirtualHost > config.

If you want to only redirect if there files and directories don't exists already, then this will work:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/.+ / [R,L]
If you never want to have sub-content accessible then remove the condition rules
RewriteEngine ON
RewriteRule ^/.+ / [R,L]

Related

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

CodeIgniter: add custom rewrite rule to redirect from static file to controller

Lets say that I have a static app and a JSON file that feeds it.
The problem is that the JSON file it's not available yet and I can't use it, so, to make the application run in development environment, I want to add a rewrite rule to redirect the JSON request to a CI controller path that will return a JSON as well.
My htaccess file looks like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|assets|static|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Rewrite for development
RewriteRule ^static/app/json/appData.json$ /buscador/getAppData
The first condition is the CI main condition to redirect all request to index.php
Then I added the RewriteRule for the JSON file that's going to be located in static/app/json/appData.json and it needs to be redirected to the CI controller buscador/getAppData
The thing is that this Rewrite Rule isn't working and I don't know why, can somebody tell me what is wrong o how can I add a custom rewrite rule in CI?
Thank you!
I was pondering on a similar problem for quite a few hours. I needed to add a custom RewriteRile so that a shorter link should be generated (and working) in order to be sent as a footer in an SMS to users. I finally managed to do it with a 301 redirect before the CI redirects. Here's my .htaccess:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#Custom redirect
RewriteRule ^nv([0-9]+?)?$ /news/locate/item/$1 [NC,L,R=301]
#Remove trailing slash against SEO duplicates
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
#Generic controller redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://domain.tld/nv1234 redirects to http://domain.tld/news/locate/item/1234 as intended, although not as transparently as I would like it to, but I couldn't find a better way.
Please note the R=301 in the Custom redirect line - the RewriteRule will not work if the client is not redirected to the newsController.
Posting my solution here with the hope that other developers will not get lost as I (and the OP) did.

.htaccess redirect subdomain to php file without URL change in browser

help me please.
I have several php files in root
domain.com/dog.php
domain.com/cat.php
...
When user go to dog.domain.com i need to show domain.com/dog.php but keep dog.domain.com in browser URL
I already have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteRule .* http://domain.com/%1.php [L]
It works fine and show domain.com/dog.php content when I type dog.domain.com, but now it changes URL in browser to domain.com/dog.php
How to solve it?
P.S. Wildcard DNS record turned ON (already catch all subdomains *.domain.com)
If *.domain.com is defined within the same vhost you have to use a relative RewriteRule there: RewriteRule ^.*$ /%1.php [L]). If not, you have to replace [L] by [P] and make sure that you have mod_proxy loaded. See https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Last but not least, you might want to update your RewriteCond to be case insensitive: RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

SocialEngine - 301 redirection of old site pages .htaccess

I installed SocialEngine to replace an old CMS. The problem is that a lot of old URLs are present in the search engines. I would like to redirect them to the corresponding URLs on the current website (because at this time they only redirect to the homepage).
All the content of my old CMS was imported into SE, so each old page has its equivalent now.
For example :
old address : /index.php?mod=news&ac=commentaires&id=367
new address : /articles/367
I tried to add in my .htaccess something like :
redirectmatch 301 index.php?mod=news&ac=commentaires&id=([0-9]+) http://www.nailissima.fr/articles/$1
But it doesn't work. When I type the old address, it still redirects to the homepage, not to the corresponding article.
Would you please help me to solve that ?
Here is my .htaccess :
# $Id: .htaccess 7539 2010-10-04 04:41:38Z john $
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size
Thank you in advance !
Best regards
Possibly you have the problem with order of your redirects instructions.
First disable with comments all your current general rules and try your new redirects alone.
If they work, then attentivly think where you should place them.
[L] at the end of instruction makes it last, so it this instruction matches, not further will process.
Using this, write more detailed instructions first, and more general later, otherwise, you can meet situation when further instructions overright your current!

Resources