1

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.

I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.

http://mywebsite.com/subfolder/index.php?v=variable

to

http://mywebsite.com/subfolder/variable

The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php

How can I get this to work? Any help gratefully appreciated.

2 Answers2

1

Use this in your .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]

This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.

I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.

You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6

Make sure you clear your cache before testing this.

Joe
  • 4,877
  • 5
  • 30
  • 51
1

You can use these rules inside your /subfolder/.htaccess

RewriteEngine On
RewriteBase /subfolder/

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]

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

Update (passing two values)

RewriteEngine On
RewriteBase /subfolder/

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]

# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]

# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • Thank you! It's worked perfectly. I was wondering though if you could help me modify it so that if a second variable was used e.g. http://mywebsite.com/subfolder/index.php?v=variable&v2=variable2 that it would still appear as http://mywebsite.com/subfolder/variable – Owen Garfield Jul 29 '18 at 16:01
  • I updated the code to remove a restriction, you can now have other key-value pairs after `v=variable`. All will go to `/subfolder/variable` (keeping no track of `v2=variable2`). Anyway, be aware that it's impossible to rewrite `variable2` back since it does not know anything about it when coming from `/subfolder/variable` – Justin Iurman Jul 29 '18 at 17:00
  • Thank you. That's great. I really appreciate you sparing the time to help me. Do you know if there is a simple way to pass two values? This is partly because I'm using this approach to use includes to construct the pages, but also want to pass other variables over to allow the information the page shows to be controlled with other variables (in this case it's a vocabulary list and I'd like to show only the nouns, adjectives, etc if chosen) – Owen Garfield Jul 30 '18 at 16:15
  • You can check the `update` part of my answer. If you have an infinite (I mean, a lot) of variables, I'm afraid you need a rule for each – Justin Iurman Jul 30 '18 at 16:48