Max Njoroge
Max Njoroge

Reputation: 487

mod_rewrite not working htaccess

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>

Upvotes: 1

Views: 1133

Answers (3)

Max Njoroge
Max Njoroge

Reputation: 487

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.

Upvotes: 0

user2493235
user2493235

Reputation:

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.

Upvotes: 2

cnst
cnst

Reputation: 27238

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

Upvotes: 1

Related Questions