Rewrite affecting multiple directories. Also need help with a rewrite rule

This is my htaccess file on the root

Options +SymLinksIfOwnerMatch -MultiViews

ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.codefundamentals\.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L]

RedirectMatch ^/$ /index

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.+/([\d]+)(\/)?$ /index.php?index=$1

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Redirect 301 /portfolio.php /hire
Redirect 301 /portfolio /hire

I am making a sort of a-z index for HTML/CSS on my website. I made a “reference” folder and I have an index.php page in it. I want to be able to do index.php?reference=a and have the URL be rewritten to codefundamentals.com/reference/a/ (or if I point to this URL, I want it to work as if I went to index.php?reference=a).

However all attempts I make at this only show my homepage (root homepage) when I put in an htaccess rule and view. I believe this is because of my htacess being able to be affected due to my “index” code in my htaccess. How can my current index.php htaccess be rewritten to only affect the root directory, and then I can work on getting my reference folder htaccess working.

Okay, I need a better understanding :smile:

Visiting index.php?reference=a goes to /index?

So ideally

.com/reference/index

That link will have my a-z index links. I’d like them to be able to be appended to the URL. So

index.php?reference=a.

That should redirect to.

.com/reference/a/ (or at least allow this URL to work.)

Likewise, I will be having pages like this.

.com/reference/a/after (for the ::after pseudo element)

This will be the URL that that page needs to read from

.com/reference/index.php?reference=a&page=after

Does that make more sense? Not so much needing the redirect, as I need the beautiful URLs :wink: .

I think so, just to be clear index.php?reference=a doesn’t do anything on your site today right? At least from what I can tell it doesn’t. I simply want to make sure we don’t have an underlying problem of “that link should be working, but an existing rule is breaking it” that we must deal with first.

http://www.codefundamentals.com/reference/index.php?reference=a

That does nothing special.

However if you visit the URL that this needs to work with

http://www.codefundamentals.com/reference/a/

That’s a 404.

I tried taking a my rewrite code in my htacess currently and writing it for the reference folder but my regular index code is overriding it.

I could show you my attempt? I had deleted it after it failed.

Then there’s also the fact I need something like this to work.

http://www.codefundamentals.com/reference/a/after

Whereas this would be the URL that URL bases its content off of

http://www.codefundamentals.com/reference/index?reference=a&page=after

And do you want http://www.codefundamentals.com/reference to go to http://www.codefundamentals.com/reference/index ?

That doesn’t matter to me. Preferably no.

I wanted the index redirect before on the base folder because the pagination URLs looked better and more seamless.

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference/([a-z])(\/)?$ /reference/index.php?reference=$1

Updated htacess.

Options +SymLinksIfOwnerMatch -MultiViews

ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.codefundamentals\.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L]

RedirectMatch ^/$ /index

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.+/([\d]+)(\/)?$ /index.php?index=$1

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Redirect 301 /portfolio.php /hire
Redirect 301 /portfolio /hire

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference/([a-z])(\/)?$ /reference/index.php?reference=$1

Unfortunately it doesn’t seem to change anything. Tested with this.

http://www.codefundamentals.com/reference/a/after

May need to remove the leading / in front of the word reference (that was the only part I was questioning myself on)

I did and it doesn’t seem to have changed anything. Updated rules below. I might not have done what you intended though?

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/reference([a-z])(\/)?$ /reference/index.php?reference=$1

To

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^reference/([a-z])(\/)?$ /reference/index.php?reference=$1

Still seem to be getting 404 error though. Sorry :frowning: .

http://www.codefundamentals.com/reference/a/after

Okay remove the RewriteCond statements…

RewriteRule ^/reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2
RewriteRule ^/reference/([a-z])(\/)?$ /reference/index.php?reference=$1

Good try, but 404 :stuck_out_tongue: .

Options +SymLinksIfOwnerMatch -MultiViews

ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.codefundamentals\.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L]

RedirectMatch ^/$ /index

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.+/([\d]+)(\/)?$ /index.php?index=$1

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Redirect 301 /portfolio.php /hire
Redirect 301 /portfolio /hire

RewriteRule ^/reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2
RewriteRule ^/reference/([a-z])(\/)?$ /reference/index.php?reference=$1

Ah, now remove the leading slash (didn’t realize the online tester was still showing it as “not met”)

RewriteRule ^reference/([a-z])/([a-z-]+)(\/)?$ /reference/index.php?reference=$1&page=$2
RewriteRule ^reference/([a-z])(\/)?$ /reference/index.php?reference=$1
1 Like

Ding ding ding! Thank you!

I have no idea why the RewriteCond wasn’t working… :frowning: But at least it works now :smile:

What online tester do you use for this @cpradio ? I used a bunch from Google and they ultimately all suck.

Also, you know how CSS is mainly debugged from inspect element? What’s the JS equivalent? I use the console and put console logs in my code to see what’s happening (alerts too) but I dunno if there is a better way (OT I know).

cp,

Aw, (re post #16) I KNOW that you know better than to:

  1. escape the slash character in your regular expressions (it doesn’t need it).
  2. redirect to the DocumentRoot with a / (that causes Apache to look to ITS root first so that’s a great advantage for hackers); It’s also not necessary if this .htaccess (mod_rewrite code) is located in the domain’s DocumentRoot.
  3. using optional trailing slash characters will cause all internal links in the new {REQUEST_URI} to have two options for directory level of the request (meaning either the / version will be correct and the non-slash to fail to find internal links or visa versa). IMHO, absolute internal links are a terrible way to resolve this issue as I have never found any reason to have an optional trailing slash (except when redirecting to a DIRECTORY where that it the technically correct way to specify the link).

Regards,

DK