php - One url with two different paths (url rewriting)

115

I have a url as following

http://example.com/post/post-title/1

so it is /file-name/post-name/page-number

so i want the 1st page to display without page number.

http://example.com/post/post-title

and then from second page

http://example.com/post/post-title/2

My htaccess code is.

RewriteRule post/(.*)/([0-9]+)$ post.php?id=$1&page=$2 [NC,L]

Is there any way to do this? Applicate your help.

324

Answer

Solution:

You can have 2 rules for this:

Options -MultiViews
RewriteEngine On

# default rule for page #1
RewriteRule post/([\w-]+)/?$ post.php?id=$1&page=1 [NC,L,QSA]

# to handle URLs with specific page #
RewriteRule post/([\w-]+)/(\d+)/?$ post.php?id=$1&page=$2 [NC,L,QSA]

People are also looking for solutions to the problem: php - Filter multidimensional array using multiple rules from another multidimensional array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.