Not sure where I go wrong with rewriteRule?

I’m trying to pass 2 variables along with my rewriteUrl but something goes wrong.

I want to be able to write this url:

As it is now i’m able to do everything untill the blogtitle:

Using this:

RewriteEngine On
RewriteRule (.*)$1 [R=301,L]

RewriteRule ^blog/([a-zA-Z0-9_-]+)/?$ blog.php?profile=$1

I then get the variable in my php like this:

$username= urlencode($_GET['profile']);
//$seotitle= urlencode($_GET['title']);

As you can see I want to be able to also get the seotitle for the blog… How do I do that?

Thanks in advance…


The problem you’re having is not starting out properly … by creating a “specification” for your mod_rewrite code. Okay, you actually DID but then you promptly ignored it with your code!

To do that, you’ll need to capture BOTH the username and blogtitle and your mod_rewrite code only captures one (and does NOT match them both, i.e., ignores your mod_rewrite code).

RewriteEngine On
RewriteRule (.*)$1 [R=301,L]
[indent]Just what does this "Lazy Regex" do but loop?

[standard rant #1][indent]The use of "lazy regex," specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]
RewriteRule ^blog/([a-zA-Z0-9_-]+)/?$ blog.php?profile=$1
[indent]Well, the atom is fine for capturing either the username or the blogtitle but CANNOT capture both. Moreover, the optional trailing slash will generate a problem with your relative links so, if you're in charge of creating the redirections, get rid of that trailing slash![/indent]

What to do for both?

  1. Decide which variable you MUST have

  2. Decide which will come first in your links

  3. Decide which characters will be allowed in each value (for these keys)

  4. Code:

RewriteEngine on
RewriteRule ^blog/([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?$ index.php?user=$1&title=$3 [L]

Okay, okay, I know I changed the key names but I’ll let you sort those out. Obviously, the first key is the username which I made MANDATORY but the second, the title or seoname or blogtitle or …, is now OPTIONAL and is the THIRD atom created with the regex.

Note no trailing slash. Note, too, that index.php must know where it’s located (hopefully, your CMS has already accommodated that).