Bynd
Bynd

Reputation: 705

Change home page url using htaccess

In my framework, the home page is

http://localhost:8585/web1/dashboard

I want to change that url of the home page to

http://localhost:8585/web1/

I've tried both RewriteRule ^dashboard(.*)$ $1 and RewriteRule ^(index)$ /dashboard [L] but it didn't work.

Upvotes: 0

Views: 58

Answers (1)

Edwin
Edwin

Reputation: 1135

Let's dive in to the rules you have provided!

RewriteRule ^dashboard(.*)$ $1 Looking at the (.*) The meaning of the dot is that it can be any character (but only one). So .* means that it can be a lot of character. This is typically used to capture the entire requested url, without the domain. The $ at the end means quite literally that the host ends with the requested url (remember; that's without the domain) thus making no sense. How you should use these rules in a 301 redirect:

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] 

In this example we redirecting any traffic that is hitting our server, capturing the requested url and redirecting it to example.com with the captured url passed down as a parameter $1.

RewriteRule ^(index)$ /dashboard I can see where you are going with the second rewrite url. Trying to capture the index group and redirecting it to dashboard. In itself is a valid way to describe it but wrongly implemented. Based on our first explanation the (index)$ group states the requested url has to be index or else this rule won't be met. So.. that means it's never met because the url never will be just index.

What you have to do: If you just want to redirect the dashboard page to web1/ you use a rule as follows:

RewriteRule    ^web1/dashboard/?$    web1/   [NC,L] 

This rule works for rewriting "http://localhost/web1/dashboard" to "http://localhost/web1/". However if you have pages behind the dashboard that need to be redirected as well you can apply this rule:

RewriteRule    ^web1/dashboard/(.*)$    web1/$1   [NC,L] 

Can you spot the difference? ;-)

Some sources:

Upvotes: 1

Related Questions