2

Slight tweak on some other questions/answers I found here, which didn't work for me.

My website had a subdirectory of www.mywebsite.eu/subdirectory where all the website's pages were located. I have now moved all the files to www.mywesbite.eu. The /subdirectory has essentially been deleted.

How can I do a .htaccess redirect that makes any /subdirectory URL redirect to a URL without the subdirectory.

Here's an example: www.mywebsite.eu/subdirectory/webpage.html now needs to be www.mywebsite.eu/webpage.html

Instead of doing a redirect on all the pages on my website, I'm hoping there's an easier way to just say "if someone requests a URL with /subdirectory in it, take them to exactly the same URL, just without /subdirectory at the start"

I originally tried

RewriteRule ^subdirectory/(.*)$ www.mywebsite.eu/$1 [L,R=301]

but that didn't work. I have this at the very start:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
anubhava
  • 761,203
  • 64
  • 569
  • 643
Mr N Dynamite
  • 477
  • 7
  • 21
  • 1
    I originally tried `RewriteRule ^dubdirectory/(.*)$ www.mywebsite.eu/$1 [L,R=301]` but that didn't work. I have this at the very start: `Options +FollowSymLinks` `RewriteEngine On` `RewriteBase /` – Mr N Dynamite Feb 23 '15 at 14:51

1 Answers1

3

You can use this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^subdirectory/(.*)$ /$1 [L,NC,R=302,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • ok strange. I've tested it on a few links and the URL is being rewritten perfectly, but it's showing an error saying: `The requested URL /mypage.html/ was not found on this server.` – Mr N Dynamite Feb 23 '15 at 15:17
  • If I enter that URL directly in my browser it loads fine. Not sure why the trailing / is there. There must be a rule in the .htaccess doing it. Unfortunately I'm picking this website up from someone else so there is some tidying to do – Mr N Dynamite Feb 23 '15 at 15:21
  • Yes that means you have a faulty rule adding trailing slash to every URI. If you show your full .htaccess in question I can suggest something. – anubhava Feb 23 '15 at 15:22
  • 1
    Think we've (you've) cracked it. `rewritecond %{REQUEST_FILENAME} !-f rewritecond %{REQUEST_URI} !(.*)/$ rewriterule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]` was in my .htaccess forcing the slash. I've changed it to `RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301]` and the slash has gone and the RewriteRule you gave me above is working perfectly. Thanks anubhava! – Mr N Dynamite Feb 23 '15 at 15:26
  • Hello @anubhava I got a similar problem and I tried your rule but without success, do you have some time to see my question? – Sophie Nov 18 '21 at 16:39
  • @Sophie: Is this your question? https://stackoverflow.com/questions/69999914/dynamic-htaccess-rule-redirect-root I see an answer already there. Is that not working for you? – anubhava Nov 18 '21 at 16:40
  • Yea it is, nop the arkascha rule's doesn´t work =[ – Sophie Nov 18 '21 at 16:47
  • The rule which I have created works fine, but I need to transform it in a dynamic, I cant create a new line each time I add a new file into the folder – Sophie Nov 18 '21 at 17:44
  • I have posted an answer on that question. Try it after clearing your browser cache. – anubhava Nov 18 '21 at 18:36