2

I have a couple of urls I would like to remove the entire directoy section from a url. What would the htaccess 301 redirect be? Can someone assist.

Eg.

https://example.com/first-lastname-word1-word2/biography/firstnamelastname
https://example.com/first-lastname-word1-word2/activity/firstnamelastname
https://example.com/first-lastname-word1-word2/announcements/firstnamelastname
https://example.com/first-lastname-word1-word2/wall/firstnamelastname

To be https://example.com/activity/firstnamelastname https://example.com/biography/firstnamelastname etc etc.

anubhava
  • 761,203
  • 64
  • 569
  • 643
ivias
  • 249
  • 1
  • 3
  • 15

1 Answers1

2

Using mod_rewrite keep this rule very first rule just below RewriteEngine line:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(memorial-photos|videos|images|category)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]+/[^.]+)$ /$1 [L,NE,R=301]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This doesnt seem to do the trick. I get redirect to just the subdomain and no css files load. – ivias May 10 '15 at 00:29
  • Okay. This seems to do the trick however it conflicts with other urls that i have such as https://example/videos/video/myvideo.html. It makes it https://example.com/video/myvideo.html. How can I opt out of certain directories I can define. – ivias May 11 '15 at 17:17
  • It doesnt work for the same reason i mentioned above. I would like it so doesnt have the effect for certain directories such as videos, – ivias May 11 '15 at 18:37
  • check updated code to skip some directories from this rewrite – anubhava May 11 '15 at 19:57
  • OKay the code does work however i have some urls which are longer and unfortunately would not work with the rules. Eg. https://example.com/memorial-photos/category/3064/tylernickelson would show as https://example.com/3064/tylernickelson even if i tell it to skip /memorial-photos/ – ivias May 11 '15 at 21:45
  • There is no way to stop that since above rule will redirect twice. First `/memorial-photos/category/3064/tylernickelson` to `/category/3064/tylernickelson` and then `/category/3064/tylernickelson` to `/3064/tylernickelson` . May be a fix will be to replace first `RewritCond` to `RewriteCond %{REQUEST_URI} !^/(videos|images|category)/ [NC]` – anubhava May 11 '15 at 21:51
  • The way the rule is make is stay as /category/3064/tylernickelson without the memorial-photos – ivias May 11 '15 at 23:35
  • Didn't undertake your comment. You can use my suggested rewrite condition in previous Comment. – anubhava May 12 '15 at 03:15
  • Well Id like it to skip memorial-photos, right now its removing memorial photos still so the pages till dont render but leaving category. Would this work? RewriteCond %{REQUEST_URI} !^/(videos|images|memorial-photos/category)/ [NC] ?? – ivias May 12 '15 at 20:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77656/discussion-between-anubhava-and-ivan-vias). – anubhava May 12 '15 at 20:51