2

I'm trying to redirect my links like this:

my href = example.com/?p=users&page=2
my URL should be = example.com/users/page/2

At this moment, my mod_rewrite is working with:

my href = example.com/?p=users
my URL = example.com/users

Here is my htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
</IfModule>
2
  • The first thing you should do is change your href to your desired (canonical) URL. Otherwise (once implemented) you will end up redirecting every request to the canonical URL (potentially slow for users, twice the server hit, etc.). Commented Sep 15, 2016 at 17:18
  • 1
    my href is already the canonical (users/page/2) but not working yet Commented Sep 15, 2016 at 17:35

2 Answers 2

2

Have your complete .htaccess as this:

Options +FollowSymLinks
RewriteEngine On

# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# skip index.php from any rules below
RewriteRule ^index\.php$ - [L,NC]

# handle /users/page/2 URI
RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/?$ index.php?p=$1&$2=$3 [L,QSA]

# handle /users URI
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

4 Comments

I Don't know what is going on but when i click at my href, I'm redirect to the correct page, but without my styles and other things... my href example.com/user/page/2
To fix css/js/images path, you can add this just below <head> section of your page's HTML: <base href="/" />.
Nope! I've tried <base href="/"> and also RewriteBase / at my htaccess
Solved. At my <link rel> and <script> in my index I added a slash in the beggining of all directories! Thanks!
1
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

Try changing your RewriteRule to:

RewriteRule ^(.+?)(?:/page/(\d*))?$ index.php?p=$1&page=$2 [L,QSA]

A slight caveat of this "one rule" approach is that a request for example.com/users will be rewritten to index.php?p=users&page=, ie. an empty page URL param.

(.+?) - The question mark in this subpattern makes the regex non-greedy, otherwise the optional second part of the regex will always be omitted.

2 Comments

Not working, actually... I think I'll let this the way it is. This thing is driving me nuts, Thanks, at all.
Sorry, I missed the "page" path segment! I've updated my answer. (It would have previously handled /users/2, not /users/page/2 - corrected.)

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.