.htaccess rewrite Category/page/1 - .htaccess

this is my current code in .htaccess
# Rewrite for category.php?cat=xxxxx
RewriteRule ^category/([0-9a-zA-Z-]+) category.php?cat=$1&page=$2 [NC,L]
I'm getting category/dog/1
But how can I achieved like this:
category/dog/page/1

Here's the answer after reading and testing it;
RewriteRule ^category/([0-9a-zA-Z-]+)/page/([0-9a-zA-Z_-]+) category.php?cat=$1&pagi=$2 [NC,L]

Related

.htaccess rewrite URL with additional question mark “?”

With .htaccess i can't get working such link:
/quiz/results/solution-one/?email=myemail#domain.com
into
/results.php?level=4&email=myemail#domain.com
(solution-one should turn into level=4)
I have spend a few hours building .htaccess code:
Rewriterule ^quiz\/results\/solution-one/(.*)/?$ /var/www/domain.com/public_html/results.php?level=4$1 [QSA,L]
Or this one:
Rewriterule ^quiz\/results\/solution-one/([=#.a-zA-Z-]*) /var/www/domain.com/public_html/results.php?level=4&$1 [QSA,L]
But it doesn't work.
Thanks in advance!
Try with:
RewriteEngine on
Rewriterule ^quiz/results/solution-one/?$ results.php?level=4 [NC,QSA,L]
Without file path, but relative to root. And without test for query string (not part of Rewriterule test uri)

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

.htaccess rewrite with GET param

Im trying to transfrom link like this:
http://localhost/photohosting/user/view.php?img=60
Into something like that in users browser:
http://localhost/photohosting/60
Here my code for .htacces
RewriteEngine on
RewriteRule ^view.php(.*)$ /photohosting/user/view.php?img=$1 [L,QSA]
I'm not familiar with .htaccess so I can't found the mistake. Why this code doesn't work?
UPDATE: I've updated my .htaccess to:
RewriteRule ^([a-zA-Z0-9_-]+)$ user/view.php?img=$1 and now link like http://localhost/photohosting/60 works, but it misses param.
Try with the following rule:
RewriteRule photohosting/([0-9]+)$ /photohosting/user/view.php?img=$1 [L,QSA]

htaccess : rewrite urls

please help to rewrite Urls from
http://www.site.com/index.php?cat=catname
and
http://www.site.com/index.php?id=productname
to be like this
http://www.site.com/catname
and
http://www.site.com/productname
i think it will require php check if the page is cat work with it as cat
or its product work with it as product
in .htaccess file:
RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ /index.php?id=$1 [L,QSA,NC]
category/product check must be done in index.php...
Or you can add extra slug
RewriteRule ^(category)/([0-9a-z\-\_]+)?$ /index.php?category=$1 [L,QSA,NC]
RewriteRule ^(product)/([0-9a-z\-\_]+)?$ /index.php?product=$1 [L,QSA,NC]
but url will look like http://site.com/category/catname
http://www.site.com/catname and http://www.site.com/productname
The problem with that scheme is that you can't tell whether it's a catalog name or a product name by the URL. As a result, you'll probably want something like this:
http://www.site.com/Catalog/catname and http://www.site.com/Product/productname
Which can then be implemented in a .htaccess file with the following rules:
RewriteEngine On
RewriteRule ^Catalog/(.+)$ /index.php?cat=$1 [L]
RewriteRule ^Product/(.+)$ /index.php?id=$1 [L]

Resources