0

I'm still finding difficulty rewriting the htaccess to make the url a bit pretty.

this is the actual url:

http://mydomain.com/folder/page.php#_=_

I want to rewrite the above to:

http://mydomain.com/folder/page

I tried this approach after following various tutorials but its not working:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ hhttp://mydomain.com/folder/page.php#$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ http://mydomain.com/folder/page$1
</IfModule>

I know its wrong but I really don't know how to do it properly.

any help is greatly appreciated. TIA

  • The regex you have `^([a-zA-Z0-9_-]+)$` only matches a single alphanumeric string, no `/` slashes, like your path `folder/page` has. Also the full URL `hhttp://mydomain.com/folder...` (notice the double `h` btw) will lead to a redirect, not an internal rewrite. – mario Mar 22 '14 at 05:48
  • Wont work! You have NO Access on Hashtags like **#**. You can only check this on client side with javascript. Your server will never get data after **#** – Adrian Preuss Mar 22 '14 at 05:52
  • check this link i hope this will help you http://stackoverflow.com/questions/15917258/remove-php-from-urls-with-htaccess – Dexter Mar 22 '14 at 05:52

2 Answers2

0

#_=_ won't be passed to the server (actually it's not a part of URI).

The only way is to do it with JavaScript (on client side) instead of with PHP (on server side):

if (location.hash === "#_=_") {
    location.replace(location.href.replace(/#.+/, ""));
}

But it seems not necessary.

lujjjh
  • 473
  • 4
  • 9
  • will the code still work if my url was `http://mydomain.com/folder/index.php#_=_` – nancylee973 Mar 22 '14 at 11:52
  • @nancylee973 The only effect of the code is to remove `#_=_` from `location.href`. In your example, it will redirect to `http://mydomain.com/folder/index.php`. – lujjjh Mar 22 '14 at 12:00
  • @nancylee973 If you want to shorten your URL, you can use the code together with URL rewriting. URL rewriting will remove `index.php` from the URI, and the code can remove `#_=_`. – lujjjh Mar 22 '14 at 12:02
  • can you share some example? – nancylee973 Mar 22 '14 at 12:06
  • @nancylee973 I thought you'd like to redirect to `http://mydomain.com/folder/` instead of `http://mydomain.com/folder/index.php` in your example. If not, you don't need to do this. – lujjjh Mar 22 '14 at 12:35
  • yes I would greatly appreciate if you can show me an example of how to redirect to `http://mydomain.com/folder` via htaccess. – nancylee973 Mar 22 '14 at 12:50
  • @nancylee973 `RewriteRule ^folder/index.php$ folder/ [L,R=301]` Very simple, isn't it? You can improve it yourself. – lujjjh Mar 22 '14 at 12:58
-1

Use this

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^folder/page.php#_=_$ folder/page [L,R=301]
</IfModule>