How can I rewrite all urls to "/"? - html

Server: Apache
I'm looking to rewrite my urls in the following way, and I can't figure out a way to get it to work.
http://website.com/index.html
I want it to redirect to:
http://website.com/
So basically I want to load index.html, but not display it in the url. I also don't want to give it another name. I'm not looking for /index.html to become /index
Additionally, I'm trying to have any GET data such as:
http://website.com/index.html?id=0&name="fred"
To rewrite to:
http://website.com/?id=0&name="fred".
If you have a solution that is not using .htaccess files that's fine as well.

I believe this should work:
RewriteEngine on
RewriteRule ^index\.html(.*)$ /$1 [R=permanent,L]
This will redirect any requests that start with /index.html to simply "/" and will preserve any arguments that come after index.html

For a .htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /index\.html
RewriteRule ^index\.html$ / [L,R=301]

Related

Need to redirect missing URLs to new host

I'm moving a blog from one site to another and repurposing the original site. I want to maintain all existing links that point to the site and hopefully maintain SEO page ranking.
Old URL: http://www.companyabc.com/2010/04/test.html
New URL: http://blog.companyabc.com/2010/04/test.html
The way I'd like to do it is to use a custom 404 error page on www.companyabc.com like this:
<html><meta http-equiv="REFRESH" content="0;url=http://blog.companyabc.com/%1"></html>
where %1 is the original URI (/2010/04/test.html), but I don't know if that's possible.
Another option is to use an .htaccess file that redirects if the URL is not found, but I haven't gotten that to work either. I'm sure I'm doing something wrong in the rewrite condition:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.*)\.html$ https://blog.companyabc.com/$1.html [R=301,L]
Any suggestions? Thanks for the help.
I got it working using the following .htaccess configuration:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://blog.companyabc.com/$1 [R=301,L]
I never tried the REQUEST_FILENAME value because I mistakenly thought it only applied to files that can be downloaded from the website. I didn't realize it also applies to the .html files in the blog.
With this solution all URLs that don't exist at www.companyabc.com will be redirected to blog.companyabc.com instead of showing a 404 error page, which is what I'm looking for.

Using .htaccess to RewriteRule but every file points to one file

I am currently trying to using .htaccess to do the following:
When b/ is found in the url, it redirects to browse.html. My .htaccess file looks like:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^b/(.*)$ browse.html [NC,L]
I am using Apache version 2.2.31 and here is what my directory looks like:
Root
Root/browse.html
Root/[css]
Root/css/browse.css
Root/[js]
Root/js/browse.js
When I do this, it brings me to browse.html but nothing loads correctly. Every file in the css and js directories become browse.html. Is there something extra I need to add to this?
Here is what the dev tools directory looks like with the .htacess
localhost
b/?page=1 (browse.html file. Name comes from query string)
b/[css]
b/css/browse.css (blank file)
b/[js]
b/js/browse.js (browse.html instead of browse.js)
I have tried putting RewriteBase / in between but that does nothing to the problem.
Your rewrite rule is fine. Problem is happening due to your use of relatives paths for css/js/images. While resolving relative paths browser appends it to current page's path.
You can add this rule above your existing rule::
RewriteEngine On
# fix css/js paths
RewriteRule ^b/.*((?:js|css)/.+)$ /$1 [NC,R=301,L]
RewriteRule ^b/(.*)$ browse.html [NC,L]
Make sure to ignore requests if files/folders exists:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^b/(.*)$ browse.html [NC,L]

url rewriting to hide a directory

My website directory contains 2 folders : website1 and website2
So to access them:
http://www.example.com/website1
http://www.example.com/website2
I want to access them with these URLs:
website1: http://www.example.com
website2: http://www.example.com/website2
so I write this .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ website1/$1 [L]
This works well for website1 but if I want to access website2 it doesn't work (because it redirects to http://www.example.com/website1/website2 I think)
How I can write this file to redirect only when URL is http://www.example.com?
You can either:
Create another .htaccess in the /website2 and disable the rewrite engine:
RewriteEngine Off
This will prevent the mod_rewrite directives in the parent .htaccess from running, so the request is not rewritten.
Or,
Modify the existing mod_rewrite directives in the .htaccess file in the document root to exclude requests to the /website2 subdirectory. For example:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/website2
RewriteRule (.*) website1/$1 [L]
The ! prefix on the CondPattern negates the regex, so it's only successful when the REQUEST_URI does not start with /website2.
You could also combine this into a single directive, which is marginally more efficient:
RewriteRule !^website2 website1%{REQUEST_URI} [L]
Note that the REQUEST_URI server variable already contains the slash prefix, so this must not be duplicated in the RewriteRule substitution.
I think you don't need htaccess for this. Put your website1 files in main root. Create a new folder and name it website2. Put website2 files in this new folder. I think this is what you want.

Re-write links only not redirect

Hi I don't know much about htaccess and can't understand how the codes work.
Can someone help me do this
www.site.com/forums/index.php?/cp/3-welcome/
will only change the text itself to
www.site.com/forums/welcome
Not redirect it.
You will first want to ensure rewrite rules are enabled. You can do this through your .htaccess file using code similar to:
<Directory /var/www/website/html>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ... ...
RewriteRule
</Directory>
Once that is done then you should be able to run your RewriteCond command so that it will successfully redirect as the rewrite engine will be enabled.
Your redirect within the .htaccess file should look something like:
RewriteRule ^http://([^/]*)/forums/welcome(\d+)/(.*)$
http://$1/index.php?/cp/3-welcome/
Of course you may need to update/edit this as required for your specific needs, but this should at least give you a general idea.

Clean/Short URLs using .htaccess is not working as it should

I've been following some YouTube videos to try and get this working but it doesn't seem to want to. This is how my .htaccess file is looking:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule RewriteRule ^(.*)$ index.php/$l
I am completely new to .htaccess, I'm not completely sure how it works but my common sense tells me to just create a file, call it .htaccess and place it in the same folder as my index.php, right?
So when I go to, say index.php/foo/bar or even just /foo/bar/ I get a 404 error.
What are the possible problems? It's a Windows 2008 Server with PHP 5.3.10 installed.
As beginning your last line should be
RewriteRule ^(.*)$ index.php/$l
And it should work as you expect it... pass all requests but ones for existing files/folders into index.php
The .htaccess can be in any folder which is hit by the request URL... the only difference is the base of the URL which is relative to the folder where is the .htaccess located
Example:
/folder1
.htaccess
index.php
/folder2
.htaccess
index.php
If you access /test.php none if your .htaccess files will be processed (it's not related to the request URI.
If you access /folder1/test.php the corresponding .htaccess in folder1 will be used and the $1 in your RewriteRule will show test.php, the /folder1/ will be stripped from the URI.
If you access /folder1/folder2/test.php it's similar... apache will use .htaccess from folder2... the one from folder1 will be ignored (a bit counderintuitive)