1

I have a website with 11 sub-domains.I have one main htaccess file in the public_html root folder that has the common shared traits like cache etc. I have separate htaccess files in each of the sub-domain root folders where I use mod_rewrite however I can't get it to rewrite the URL. I'm trying to change it from dundaah.com/days/content.php?day=thur to a much cleaner dundaah.com/thursday and for the sub-domains music.dundaah.com/days/content.php?day=thur to a preferred music.dundaah.com/thursday. My 1st researched code so far only gets me to the URL dundaah.com/day/thur and the 2nd to dundaah.com/thursday still gives me a 404 error. I have not changed the internal links of the site, is this a prolem? Could anyone help? Thanks

<IfModule mod_rewrite.c>

  Options +FollowSymlinks
  
  # enable the rewrite engine
  RewriteEngine On
  RewriteCond %{REQUEST_URI} ^/days/content.php
  RewriteCond %{QUERY_STRING} ^day=([a-z]+)$
  RewriteRule ^(.*)$ /days/%1? [R,L]

  # or for individual pages 
  RewriteCond %{REQUEST_URI} ^/days/content.php
  RewriteCond %{QUERY_STRING} ^day=thur$
  RewriteRule ^(.*)$ /thursday? [R,L]
</IfModule>
Max Njoroge
  • 489
  • 3
  • 21

3 Answers3

2

This will make /thursday work (and all the other days), put it in your main .htaccess and in your sub-domain ones. It will also redirect your old URLs to the new ones.

It will only work if you are on Apache 2.4 or later. Let me know if you're not and I'll update the answer.

RewriteEngine on

# Redirect old URLs
RewriteCond %{QUERY_STRING} =day=mon
RewriteRule ^days/content\.php$ /monday [R=301,END]
RewriteCond %{QUERY_STRING} =day=tue
RewriteRule ^days/content\.php$ /tuesday [R=301,END]
RewriteCond %{QUERY_STRING} =day=wed
RewriteRule ^days/content\.php$ /wednesday [R=301,END]
RewriteCond %{QUERY_STRING} =day=thur
RewriteRule ^days/content\.php$ /thursday [R=301,END]
RewriteCond %{QUERY_STRING} =day=fri
RewriteRule ^days/content\.php$ /friday [R=301,END]
RewriteCond %{QUERY_STRING} =day=sat
RewriteRule ^days/content\.php$ /saturday [R=301,END]
RewriteCond %{QUERY_STRING} =day=sun
RewriteRule ^days/content\.php$ /sunday [R=301,END]

# Rewrite new URLs
RewriteRule ^monday$ days/content.php?day=mon [END]
RewriteRule ^tuesday$ days/content.php?day=tue [END]
RewriteRule ^wednesday$ days/content.php?day=wed [END]
RewriteRule ^thursday$ days/content.php?day=thur [END]
RewriteRule ^friday$ days/content.php?day=fri [END]
RewriteRule ^saturday$ days/content.php?day=sat [END]
RewriteRule ^sunday$ days/content.php?day=sun [END]

It is best to do the days individually since the script uses a short form and the new URL doesn't. You can change them if needed, or let me know if I'm missing something.

1

Both Apache and nginx have the concept of internal and external redirects.

What you could do to accomplish your task is create a "redirect loop", where /days/content.php?day=thur would redirect/R (external redirect) to /thursday, whereas /thursday would internally go back to /days/content.php?day=thur, which may initially look like a loop.

See:

This could be done by testing against the request uri in each case:

RewriteCond %{REQUEST_URI} ^/days/content.php$
RewriteCond %{QUERY_STRING} ^day=thur$
RewriteRule ^(.*)$ /thursday? [R,L] # external redirect

RewriteCond %{REQUEST_URI} ^/thursday$
RewriteRule ^(.*)$ /days/content.php?day=thur [L] # internal redirect
Community
  • 1
  • 1
cnst
  • 25,870
  • 6
  • 90
  • 122
  • this is good, the url in the browser shows `dundaah.com/thursday` but doesn't show content just `dundaah.com redirected you too many times` and that i should try clearing my cookies but even after i did, still got the same message. the internal links still remain `days/content.php?day=thur` for the index page and `content.php?day=thur` for files inside the `days` folder, which are just two `content.php & contact.php`. i don't have any code that removes the `.php` extention. – Max Njoroge Jan 30 '17 at 06:38
  • @MaxNjoroge, did you make sure to have only one `R`? It sounds like you are creating an actual loop by having external redirects in each case, instead of having an internal one in the other one. – cnst Jan 30 '17 at 21:42
  • You can't use "REQUEST_URI" as that is updated by rewrites. What you could use is "THE_REQUEST" which is the original request and doesn't get modified. Or just use [END] as I have in my answer, so no more processing is done by mod_rewrite after a successful match. –  Jan 31 '17 at 04:42
0

My apologies, I was the one using a bad folder directory system in my website. This is the one is was using

public_html(www directory) -index.php -vidz(sub-domain) -index.php -days -_imports -css -js -php -content.php -contact.php

This is my upgraded system

public_html(www directory) -index.php -vidz(sub-domain) -index.php -content.php -contact.php -assets -_imports -css -js -php

Which makes it possible to have the links as <a href='monday'>Mon</a> and use the .htaccess redirect rules below.

<IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine On   
    RewriteRule ^monday$ content.php?day=mon [NC,L]
    RewriteRule ^tuesday$ content.php?day=tue [NC,L]
    RewriteRule ^wednesday$ content.php?day=wed [NC,L]
    RewriteRule ^thursday$ content.php?day=thur [NC,L]
    RewriteRule ^friday$ content.php?day=fri [NC,L]
    RewriteRule ^saturday$ content.php?day=sat [NC,L]
    RewriteRule ^sunday$ content.php?day=sun [NC,L]
    RewriteRule ^contact$ contact.php [NC,L]

</IfModule>

Thanks to all who answered! It helped me realize this.

Max Njoroge
  • 489
  • 3
  • 21