Regular Expression For URL Query - .htaccess

I've been using mod_rewrite in my htaccess file to rewrite my urls and I've run into a problem when doing pagination.
Here's the url for my page:
http://domain.com/concerts/features/folk-roots?page=2
htaccess file:
RewriteRule ^features/([^/]*)?page=([0-9]*)$ featureCat.php?cat=$1&page=$2 [NC,L]
It works fine without the page query, but if I want to change pages I can't figure out how to write the regular expression to grab the page #.
Any ideas would be appreciated!

You need to use RewriteCond immediately before the RewriteRule to get access to the query string:
RewriteCond %{QUERY_STRING} ^page=([0-9]*)$
RewriteRule ^features/([^/]*)$ featureCat.php?cat=$1&page=%1 [NC,L]

Related

rewrite rule to convert php querystring to html

Please help me in writing correct .htaccess code to get the wanted result.
I have the following pages:
www.mydomain.com/?exam=/hp-certification/
www.mydomain.com/?exam=/cisco-certification/
www.mydomain.com/?exam=/oracle-exam-prep/
www.mydomain.com/?exam=/faq/
www.mydomain.com/?exam=/support/
And there are many more pages but you can notice that the fixed part is: www.mydomain.com/?exam=
Instead of using query string links, I want the following links to work as above pages:
www.mydomain.com/hp-certification/
www.mydomain.com/cisco-certification/
www.mydomain.com/oracle-exam-prep/
www.mydomain.com/faq/
www.mydomain.com/support/
and so on.
your link should be like this www.mydomain.com/?exam=hp-certification/ and the rewrite rule would like this in .htaccess file.
RewriteEngine On
RewriteRule ^([^/.]+)$ /?exam=$1 [L]
You can do that by using the following rules in your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} exam=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/%1? [R=301,NC,L]
So what does the above do?
First, it will take the query exam= as a condition, if this condition is met then it will grab any version of the variable using (.+).
It will then rewrite the URL using 301 redirection to show http://www.example.com/anything/. The use of %1 is to grab the variable from exam= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.

Redirecting based on query string

Hi all I need to redirect based on query string though htaccess in Apache.
I want that all the queries that contain jjj redirect to my homepage
I used the code below but its not working could you please help me¿
Thanks in advance,
Mauri
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://www.eventosbarcelona.com [R=301,L]
Your rule redirects example.com/?jjj to example.com/?jjj I guess, You are getting redirect loop error as both urls are same. By default, mod-rewrite appends old query strings to the destination url. You need to use a ? at the end of the Rewrite destination url to discard query string.
Try this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://example.com? [L,R]
This will redirect /?jjj to http://example.com/ .

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

Htaccess rewrites causing infinite loop

I am trying to write a redirect for a PDF file in my wordpress install that has shown up in my google webmaster tools with a load of query strings after it, what I want is to have the redirect match any number or query stings after the pdf filename and just redirect directly to the file.
RewriteRule ^(.*) http://www.example.com/wp-content/uploads/2014/08/test\.pdf$ [R=301,L]
and this also (as well as numerous different iterations)
RedirectMatch 301 ^/wp-content/uploads/2014/08/test\.pdf(.*)$ /wp-content/uploads/2014/08/test.pdf?
However they either result in a continuous redirect loop, 404 error, or still leave part of the query string
the url has any number of random query strings so something that would catch anything after the filename and just redirect to the bare file is percisely what I am looking for. here is an example of the url:
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr
or
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr
and what I want to get is just
http://www.example.com/wp-content/uploads/2014/08/test.pdf
Thanks
Try something like this with mod_rewrite.
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} (.+)\.pdf$
RewriteRule ^(.*) /$1? [R=301,L]

Strip or rewrite query string from cleaned url with htaccess

What I'm looking to do is strip a query parameter from a dynamic URL that needs to also be cleaned using HTACCESS.
The url first starts as http://testdomain.com/post.php?id=3326.
The url is cleaned with a rewrite to http://testdomain.com/post/3326/.
Some links get "?ref=no_set" appended to the end
Url now needs to be redirected from http://testdomain.com/post/3326/?ref=no_set to http://testdomain.com/post/3326/
In some cases I need to add a new query parameter "?ref=no_set" to the url, but I'm wondering if it's possible to strip that query param and still be able to use $_GET in PHP to retrieve it even though it isn't being rewritten.
Any help would be appreciated!
RewriteEngine on
RewriteCond [conditional reg ex here]
RewriteRule [rewrite reg ex here] - [CO=ref:no_set:.testdomain.com]
Simple...
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://testdomain.com%{REQUEST_URI}? [R=301,L]
# ADD CODE TO HANDLE YOUR REWRITES AFTER THIS
Have a nice day! ;)

Resources