0

I am trying to use htaccess file to rewrite urls, I really have zero experience at this and everything I have done is from searching here and on google. My links have query parameters in them. I send everything to an index.php page.

I have tested all the links before trying to rewrite them and they all work in the index page. So then I created an htaccess file to rewrite the links but all I get is a blank page, none of the variables are being sent to the index page. I use $_GET in the index.php page to retrieve the variables.

Here is a list of type of links I am trying to rewrite along with a copy of my actual htaccess file, also mod rewrte is on and functioning properly in apache. Please help me change my htaccess file to do EXACTLY what I am wanting according to the sample links I have shown below, please?

The directory rewrite is fine and works accordingly, it is the links that don't.

Also, if you notice in links #1 and #2 the page parameter does not and should not appear in the rewritten links for these 2 links ONLY but a blank or empty string is still sent to my index.php.

example--

<?php

$page= $_GET['page'];
$user= $_GET['user'];
$act= $_GET['act'];
$section= $_GET['section'];
$search= $_GET['search']
$xx= $_GET['xx'];

if($page == ''){
   //Do some code
}elseif($page == 'search'){
   //Do some code
?>

Rewrite these type of links

note - "http://" is front of all of these links, stack overflow wouldn't let me add it and post.


LINK #1

localhost/notetag2/johnsmith/all/1

--REWRITE TO--

localhost/notetag2/index.php?page=&user=johnsmith&act=all&section=1


LINK #2

localhost/notetag2/johnsmith/all

--REWRITE TO--

localhost/notetag2/index.php?page=&user=johnsmith&act=all


LINK #3

localhost/notetag2/search/johnsmith

--REWRITE TO--

localhost/notetag2/index.php?page=seach&search=johnsmith


LINK #4

localhost/notetag2/pictures/1

--REWRITE TO--

localhost/notetag2/index.php?page=pictures&xx=1


Rewrite directory

pages

--REWRITE TO--

web


HERE IS MY ACTUAL HTACCESS FILE

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /notetag2/
    RewriteRule ^index\.php$ - [L]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3&section=$4 [L,NC]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3 [L,NC]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2 [L,NC]

    RewriteRule ^(.*)/$ notetag2/index.php?page=$1 [L,NC]

    RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q=$1 [L,NC]

    RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=picturesh&xx=$1 [L,NC]

    RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L,NC]


    RewriteRule ^pages/(.*) web/$1 [L]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /notetag2/index.php [L,QSA]

</IfModule>
5
  • Why does your htaccess file say "notetag2" but your question want things rewritten from/to "notetag"? Are they supposed to be different? Commented Mar 25, 2015 at 3:04
  • No they are not different. They are one and the same. I don't use copy and paste. I type everything out and just made a mistake typing it here. It has been changed now in my post. to say notetag2 and not notetag. Can you please help with my question? Commented Mar 25, 2015 at 3:10
  • where is your htacces file? Commented Mar 25, 2015 at 3:26
  • My htaccess file is inside: localhost/notetag2 Or to be more precise, it is located at: C:\Users\MyComputer\Documents\xampp\htdocs\notetag2 I am using xampp on my computer. Commented Mar 25, 2015 at 3:32
  • htaccess works as I have tried and created one and two line htaccess files and tested them, they were not complicated in what they did. Also mod rewrite works as well. What does not work is the htaccess file I wrote above, as it does some complicated things and I don't know how to do them correctly in this file! Commented Mar 25, 2015 at 3:39

1 Answer 1

1

You need to fix a couple of your regular expressions. Since your htaccess file is in your notetag2 directory, you don't need to match against the leading "notetag2".

But first, your rules are not in the right order. If you want to match /search/something, then you can't have a rule that matches anything, otherwise it'll match /search. So these rules need to be at the top:

RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ index.php?page=picturesh&xx=$1 [L,NC]

You also don't need the notetag2/ in your rule's target since that's implied because the rules are in the notetag2 directory.

So after those 2 rules, you need to take each of your other rewrites starting from the largest to smallest:

localhost/notetag2/johnsmith/all/1

--REWRITE TO--

localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all&section=1

You almost had that, just had some random (.*)/ in front of the regex, which will make it never match what you want. You want this instead:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2&section=$3 [L,NC]

Then for the next one:

localhost/notetag2/johnsmith/all

--REWRITE TO--

localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2 [L,NC]

The "search" and "pictures" should have been taken care of by the first two rules above.

Whatever other rules you had at the bottom of your old htaccess file, they need to go below these rules.

So that leaves only:

pages

--REWRITE TO--

web

Since these don't start with notetag2, you need to put these rules in a different htaccess file, the one in your document root:

RewriteRule ^pages/(.*)$ /web/$1 [L]
Sign up to request clarification or add additional context in comments.

5 Comments

If you notice in links #1 and #2 the page parameter does not and should not appear in the rewritten links for these 2 links ONLY but a blank or empty string should still sent to my index.php to represent that variable. There is no $page=tutorial in those first 2 links the $page variable in the first 2 links only should ALWAYS contain an empty string. So what you suggested does not work!.
@DavidCunningham Your question states that the page variable for those 2 links is tutorial
That was a mistake that I did when I did the others, I just forgot to correct it. Everything is correct in my post just as it is right now. Please refresh your browser and re-read my post, please? You will then notice in links #1 and #2 the page parameter does not and should not appear in the rewritten links for these 2 links ONLY but a blank or empty string should still sent to my index.php to represent that variable. There is no $page=tutorial in those first 2 links the $page variable in the first 2 links only should ALWAYS contain an empty string. So what you suggested does not work!.
Then remove the "tutorial"
I removed page=$1 from the first 2 links and moved the RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q=$1 [L,NC] to the top. It all works now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.