0

I have an issue writing some regex to go inside my htaccess file.

Basically, my site has been setup so that index.php and all other site files are not in the root (public_html) directory but instead are in http://fitnessquiz.co.uk/fitnessquiz.co.uk/

Initially I tried the following in my public_html folder:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^fitnessquiz.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.fitnessquiz.co.uk$
RewriteCond %{REQUEST_URI} !fitnessquiz.co.uk/
RewriteRule (.*) /fitnessquiz.co.uk/$1 [L]

which correctly navigates to my homepage and displays the url correctly but then when I click any link I get a "no input file specified" message. So then I tried replacing with:

RewriteCond %{REQUEST_URI} !^/fitnessquiz.co.uk/

RewriteRule ^(.*)$ /fitnessquiz.co.uk/$1 [L,R=301]

After which the site works but every url looks like this:

http://fitnessquiz.co.uk/fitnessquiz.co.uk/someotherfolder/etc.php

I've tried various htaccess regex solutions listed elsewhere on here but none seem to work, how do I accomplish both of these things i.e. redirect to /fitnessquiz.co.uk for every url but hide the duplicate url name/folder. Im on a shared server so don't have permissions to change any server/apache settings directly.

  • check out this SO [post](http://stackoverflow.com/questions/1304492/htaccess-redirect-without-changing-address-bar) – gwillie Aug 14 '15 at 08:20

1 Answers1

1

According to this answer by nuked on a previous post you could try:

RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]

This set of rules worked for me on a very similar situation. I had employed the same cure, (re-direct if calling the folder and hide after re-writing it) but I never got the order right on my own. Thus I kept seeing the page not found errors too. Below is my attempt to explain the actions, for my own learning, hopefully others too.

RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/

Is asking the question, does THE_REQUEST contain the subfolder you need to hide?

RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$

Checks if the request is for the correct host.

RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]

Rewrite the URL as one without the subfolder and call the new link in the redirected browser. Note:

  • L : Last step. Stop processing other rules
  • R=301 : After re-writing, redirect the browser to the new URL.

When the page is redirected it has no subfolder so the first RewriteRule is skipped. And then

RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$

checks if calling the right host. And then

RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]

rewrites the url that has not the subfolder to use the correct subfolder without redirecting the page, and while hiding actual subfolder from the browser. Again note:

  • L : Last step. Stop processing other rules
Community
  • 1
  • 1
ndasusers
  • 727
  • 6
  • 12