RewriteRule not working for some links in XAMPP apache - 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]

Related

RewriteRule that works with paginated pages

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

trying to get mod_rewrite to work

I have this problem where I switched CMS from the old one to the new one so I am trying to redirect urls properly.
Here are some example urls I wanted to convert:
#-1) http://www.mysite.com/?dispatch=search_data&features=hash_tag
#-2) http://www.mysite.com/index.php?dispatch=search_data&features=hash_tag
Basically both of them are identical except the first one doesn't have index.php in the url.
I got the following to code to partially work:
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^$ http://www.mysite.com/? [L,R=301]
The above code works for #-1 (when there is no index.php) but when there is index.php it doesn't work.
Any help / guidance is much appreciated.
Thanks
____________UPDATE_______________
As per request, here is what the the target url should be:
http://www.mysite.com/
What do you mean it "works"?
You should probably post an example of what you want the new url to look like given the old url.
But in any case, part of your problem is that the RewriteRule line is specifically excluding index.php and any other page on your site.
What you wrote basically tranlates to this:
if the query string contains "dispatch=...":
redirect requests FOR THE HOME PAGE ONLY (^$) to http://www.mysite.com/
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^(index.php)?$ http://www.mysite.com/? [L,R=301]
have you tried changing the second part to include the "index.php" part?
RewriteRule ^$ http://www.mysite.com/index.php? [L,R=301]

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]

htaccess reference problem

I have this htaccess code:
RewriteEngine on
RewriteBase /xm/
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^([a-z0-9]*)\.php$ index.php?page=$1
And I want that when the user goes to, for example, main.php, that this htaccess redirects to index.php?page=main, but every time, and with every page, it redirects to index.php?page=index, and page is equal index no matter what. What am I doing wrong?
EDIT: works fine with numbers (eg 4.php) but not with letters :/
It's rewriting again on the subrequest. Put [NS] to the right of it so it'll only get rewritten once.
Try this:
RewriteCond $1 !^index$
RewriteRule ^([a-z0-9]+)\.php$ index.php?page=$1

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