user1144510
user1144510

Reputation:

Remove Page Number from URL with .htaccess

I need page numbers from URLs of the form:

http://mydomain.com/index.php?showtopic=XXXX&page=XXXX&#entryXXXX

so they become

http://mydomain.com/index.php?showtopic=XXXX&#entryXXXX

where XXXX are integers

I've previously tried:

RewriteEngine on
RewriteRule ^(.*)showtopic=([0-9]+)&page=([0-9]+)(.*) http://mydomain.com/index.php?showtopic=$1$3 [QSA,L,R=301] 

but to no avail. So I shortened it to:

RewriteEngine on
RewriteRule ^(.*)&page=([0-9]+)(.*)$ $1&$3 [QSA,L,R=301] 

but still nowt. Is there anything wrong with the regex at all?

Upvotes: 0

Views: 369

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

You can't match against the query string in a rewrite rule, you need to match against the %{QUERY_STRING} var inside a rewrite condition:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^showtopic=([^&]+)&page=([^&]+)(&.*)?$
RewriteRule ^index\.php$ /index.php?showtopic=%1%3 [L,R=301]

The #entryXXXX part of the URL is a fragment, and the server actually never sees that. It's a client/browser-side only thing. Hopefully, the browser is smart enough to re-append the fragment after getting redirected.

Upvotes: 1

Related Questions