0

I am currently building a website and it hasn't gone online, I am using wamp server 2.4. I need to remove the sub directories so as to not let out the structure of my directories, I have a test site with the following folders: includes and sub.

The links to be accessed are in sub so if the user clicks on a link that goes to sub/link.php, the url shows localhost/sub/link.php..I would like it to show localhost/test/link/ without the sub showing. So far I have googled mod_rewrite in Apache, learnt about it but still unable to remove sub-directory. All I have achieved is to remove the file extension(.php).

I have searched here on Stack Overflow but the answers are not helping maybe because they use online hosted websites. The link.php has no variables, it's a simple php file that could echo stuff but it's not using any GET/POST variables.

This is the html code, i have intentionally written one 't' in 'http'..though in the code it is two..

Link

RewriteEngine On
RewriteRule ^sub/(.*)$ /$1 [L,R=301]

Take a look at this site, it's hosted on wordpress and every link that you go to is shown without any subdirectories, it's just got one trailing slash after the url, is it possible to achieve that on apache?

http://www.afdar.com

Betty
  • 11
  • 1
  • 4
  • add re write rule what you have done so far – Awlad Liton Apr 30 '14 at 10:10
  • possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – deceze Apr 30 '14 at 10:10
  • Why is it any of your concern to "let out the structure of your directories"? What are you trying to prevent here? – deceze Apr 30 '14 at 10:11
  • For starters, I would like to make the url more user-friendly. – Betty Apr 30 '14 at 10:13

1 Answers1

0

Try this in your .htaccess

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/test/sub/
RewriteRule ^test/(.*)$ /test/sub/$1 [QSA,L]

It will rewrite URLs:

http://localhost/test/sub/link.php to http://localhost/test/link.php

http://localhost/test/sub/another.php to http://localhost/test/another.php

Ravi Dhoriya ツ
  • 4,418
  • 8
  • 33
  • 47
  • It's not working, it still shows the full URL, am I doing something wrong here? I tried saving it in the root directory first then in the sub folder directory, none worked. :( – Betty Apr 30 '14 at 10:19
  • I think you misunderstood the question, the 'test' is the root directory so it should change from http://localhost/test/sub/link.php to http://localhost/test/link.php – Betty Apr 30 '14 at 10:28
  • Nope, still not working. I typed it in the url bar as well and it says 404 Not Found. I want to rewrite the url.. The html code is 'Link' – Betty Apr 30 '14 at 10:34
  • Yet a no, it still doesn't remove the sub directory – Betty Apr 30 '14 at 10:41
  • Hi, @Betty, you can try updated answer. I've tested it on my machine its working :) – Ravi Dhoriya ツ Apr 30 '14 at 11:14
  • Give me the whole code, including html, it is not working yet. As i mentioned above, my html href goes to a folder named sub and opens a file called link.php..it's not removing the sub directory from the url, i have tried all of your above methods. – Betty Apr 30 '14 at 11:34
  • Hey, you need to edit all your urls in href without `sub` directory, e.g. `http://localhost/test/link.php` it will refer to link.php in `/test/sub`. That is what rewrite mean. In browser addressbar it will show you `http://localhost/test/link.php` but it will present you page from `http://localhost/test/sub/link.php` – Ravi Dhoriya ツ Apr 30 '14 at 11:42
  • @Betty It seems you are misunderstanding what exactly rewrite rules do. They do not rewrite URLs! They rewrite incoming requests from the point of view of the web server. Have you read what I linked to above? http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained – deceze Apr 30 '14 at 11:49
  • @Log1c, i have rewritten my href attributes, it still shows a 404 Not Found. – Betty Apr 30 '14 at 11:51
  • Can u tell me URL which is giving you 404 error? the one you wrote in href – Ravi Dhoriya ツ Apr 30 '14 at 11:53
  • It should work, because here its working for me on Windows Apache, make sure you placed `.htaccess` at root directory, `path-to-www/.htaccess` – Ravi Dhoriya ツ Apr 30 '14 at 11:58
  • @deceze, why don't you help me out if you can? If you know the answer to my question, please give it to me, I am unable to grasp the concepts of .htaccess files because I have just begun learning – Betty Apr 30 '14 at 12:00
  • @Betty The answer that's given here is already correct as far as I understand what you're trying to do, and I wrote that other thing to explain the general concept to help new users like you. I can't really add anything else. – deceze Apr 30 '14 at 12:13
  • @deceze, that answer isn't working, maybe it is the correct answer but it's not working for me..if you could help point out what I am doing wrong, that would be great. – Betty Apr 30 '14 at 12:23