Cyprus106
Cyprus106

Reputation: 5780

adding tailing slash to Mod_rewrite and redirecting non-trailing slash in virtual dir

AS sometimes happens, I have created what I would say is a hot mess in my htaccess file. I've got several problems, I know I'm internally looping on occasion, and I'm not great at this anyways. I've looked through dozens upon dozens of SO questions and tried tons of solutions. The end result is that I'm trying to piece together more than one separate solution to a multi-part problem and I've made a real mess. This is exclusively been my mission for days. I'm stuck. Bad.

Trust me. I know there's got to be glaring flaws in my logic here, and I resisted asking this question since Saturday. I know this is super-oft asked. I wouldn't be asking if I wasn't dead stuck.

( rewrite all www and non-www requests to https:// ...works fine)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

(redirecting to the page with all the jewelry)
RewriteRule ^handmade-jewelry/$ jewelry-inventory-page.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
(following was a failed attempt.) 
# RewriteCond %{REQUEST_URI} !rock-jewelry/(.*)/$
(following works with no trailing slash, which is needed) 
RewriteRule ^handmade-jewelry/(.*)$ jewelry-details.php?s=$1 [L] 
(if I turn this on, it goes to a 404 looking for "handmade-jewelry/this-piece/index.php?s=thispiece/etc...")
# RewriteRule ^handmade-jewelry/(.*)?/$ jewelry-details.php?s=$1 [L]

Sorry again. I know for a lot of you, this kind of question gets redundant.

Upvotes: 0

Views: 852

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9539

Try adding the following to your htaccess file in the root directory of your site

RewriteEngine on
RewriteBase /

#( rewrite all www and non-www requests to https:// ...works fine)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

#I believe this is redundant because of the previous rule
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

#if existing file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
#do nothing
RewriteRule ^ - [L]


#if it does not end with a slash e.g. handmade-jewelry, redirect to with slash
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,R=301]

#if it does not end with a slash e.g. handmade-jewelry/some-piece, add the slash
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,R=301]

#(redirecting to the page with all the jewelry)
RewriteRule ^handmade-jewelry/$ jewelry-inventory-page.php [L,NC]

RewriteRule ^handmade-jewelry/([-a-zA-Z0-9]+)/$ jewelry-details.php?s=$1 [L,NC]

Upvotes: 2

Related Questions