RewriteRule that works with paginated pages - apache

I am trying to define a RewriteRule
Within the site I have pages like this:
index.php?pag=company&id=853
And I've defined this rule:
RewriteRule ^/([a-zA-Z0-9])/([0-9]+)$index.php?pag=company&id=$2 [NC, L]
Trying to get this result:
company/university-of-gloucestershire/1656
This rule works, however doesn't work for paginated pages, like:
index.php?pag=company&id=853&page=2
I tried:
RewriteRule ^/(.*)/([0-9]+)/([0-9]+)$ index.php?pag=company&id=$2&page=$4 [NC, L]
Trying to get to:
company/university-of-gloucestershire/1656/page/2
Can anyone point out what's wrong?

Looks like a typo. $4 doesn't exist in your example rule for pagination. Also if this is in .htaccess, the initial slash shouldn't be there. .* is greedy, so I've replaced it with any character, not a /.
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)$ index.php?pag=$1&id=$3&page=$5 [NC,L]
company/university-of-gloucestershire/1656/page/2 would become:
index.php?pag=company&id=1656&page=2
That rule needs to precede this one because you still have to catch the pages where someone's not specifying a page number:
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)(/page/?)?$ index.php?pag=$1&id=$3&page=1 [NC,L]
company/university-of-gloucestershire/1656/ and
company/university-of-gloucestershire/1656/page would become:
index.php?pag=company&id=1656&page=1

Related

RewriteRule not working for some links in XAMPP apache

RewriteEngine On
RewriteRule ^home/?$ pages/forum_html.php [NC,L]
RewriteRule ^forum-thread/([A-Za-z0-9]+)/?$ pages/forum_thread_gen_html.php?thread_id=$1 [NC,L]
I am trying to do an internal URL redirect. The first RewriteRule works fine but the second doesn't work, the page shows object not found but when I change the second line to ^forum-thread/?$ the page displays but of course no id for my page to process.
Does anyone have any idea what might be happening?
The second URL looks like this:
localhost/rootfolder/projectfolder/forum-thread/cdjd2_dsdsd4_adsda/
The URL had underscores and the pattern matching sequence I used did not have an underscore in it. I have edited the second RewriteRule as so:
RewriteRule ^forum-thread/([A-Za-z0-9-_]+)/?$ pages/forum_thread_gen_html.php?thread_id=$1 [NC,L]

Url rewriting mod_rewrite with and without query string

I'm trying to rewrite from domain.com/page/soft-15/android-26/ to page.php?cat=15&os=26 with this code:
RewriteRule ^page/([0-9]+)\-([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)$ page.php?cat=$1&os=$2
I thinks it works fine but, how can i rewrite domain.com/page/ and domain.com/page (without the last forward slash) to domain.com/page.php keeping both rules working?
ok think i got it mixing Jimp & Jon code
RewriteRule ^page/?$ page.php [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1&os=$2 [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1 [L]
This math with
domain.com/page
domain.com/page/
domain.com/page/soft-15
domain.com/page/soft-15/
domain.com/page/15
domain.com/page/15/
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/soft-15/26
domain.com/page/15/26
and so on...
Add a trailing /? to your pattern (before the $ anchor). The ? makes the / optional.
Additionally, your patterns seem backward, matching the digits before the characters. Try this:
RewriteRule ^page/(?:([a-zA-Z\d-]*)-)?(\d+)/(?:([a-zA-Z\d-]*)-)?(\d+)/?$ page.php?cat=$1&os=$2
That should match these variations like these:
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/15/android-26
domain.com/page/15/android-26/
domain.com/page/soft-15/26
domain.com/page/soft-15/26/
domain.com/page/15/26
domain.com/page/15/26/
Your backreferences don't seem to jive with the example that you're using:
/page/soft-15/android-26/
to page.php?cat=15&os=26
Your regex looks like it's matching:
/page/15-soft/26-android/
And rewriting to:
page.php?cat=15&os=soft
If you're going by your example, you'd want something like:
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1&os=$2
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1
RewriteRule ^page/?$ page.php

Htaccess URL rewrite domain with wildcard

I'm using htaccess to manage friendly urls
My friendly URL looks like this:
www.example.com/p/12345/John-Smith
12345 is the ID number, /p/ doesn't change, and the name is irrelevant bit of friendly url. My background url looks like this:
www.example.com/page.php?id=12345
My htaccess looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^p/([0-9]+)/([^/\.]+)?$ details.php?id=$1 [L]
Everything works like a charm.
However, what I really want my htaccess to look like is below. The only different is the ^(.*)/p/ versus just ^p.
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)/p/([0-9]+)/([^/\.]+)?$ page.php?id=$1 [L]
Basically, I want to ensure that only a directory of /p/ in the url triggers this particular htaccess rule and not something unintended, like example.com/gdp/12345/abc
For the life of me, I cannot figure out why this isn't working!
Thanks in advance as always.
Your rule RewriteRule ^(.*)/p/([0-9]+)/([^/\.]+)?$ page.php?id=$1 [L] has some issues.
You're not backreferencing the correct id: The first ( ) matches what is before the /p/ so $1 will backreference that, and not the id.
URI's passed through rewrite rules when they are in and .htaccess file have the leading / stripped off, so the regex ^(.*)/p/ will never match something like http://www.example.com/p/1234 because the URI that is being passed into the rule is p/1234.
The other weird thing in your rules is you redirect everything to http://www.example.com/ which seems like it would be a redirect loop, but if that works for you then that's fine.
To deal with 1 and 2 you need to change your backreference to $2 and your regex to accomodate a p/1234 URI:
RewriteRule (^|/)p/([0-9]+)/([^/\.]+)?$ page.php?id=$2 [L]
The (^|/) says that either it's the beginning of the URI (for p/1234) or it's a slash (for /something/p/1234).

Redirecting to same page with .htaccess

From my .htaccess file:
RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule ^page showPAGE.php [NC,L]
I want users going to url domain.com/showPAGE.php to be redirected to domain.com/page .
When domain.com/page is being entered, I want it to show the content of the file showPAGE.php.
Is that possible to do?
The above results an infinite redirection loop.
Thanks
You're trying to do something that's very tricky. The problem is that, by design, the RedirectRule directive always triggers again the complete set of rules. You can only get out of the loop when you obtain a final URL that does not match any of the rules and that's the tricky part since you are reusing the showPAGE.php name.
My best attempt so far involves adding a fake hidden string:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/showPAGE\.php
RewriteCond %{QUERY_STRING} !^internal
RewriteRule ^ http://%{HTTP_HOST}/page [NC,R=301,L]
RewriteRule ^page$ showPAGE.php?internal [NC,L]
It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.
The redirect from showPAGE.php to page needs to have [L] so that it will stop processing and redirect at once, rather than going on and applying other rules (which at once map it back to showPAGE.php). Try this:
RewriteRule ^showPAGE.php page [NC,R=301,L]
RewriteRule ^page showPAGE.php [NC,L]

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom