David Cunningham
David Cunningham

Reputation: 1

htaccess with multiple parameters not working,?

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>

Upvotes: 0

Views: 1436

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

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]

Upvotes: 1

Related Questions