Why is my pagination not working after mod rewrite? - .htaccess

Before rewriting my url file.php?style=foo&page=2 works fine.
After rewrite it stops working and just stays on the same page with the ?page variable at the end like it's not getting caught:
foo-new?page=2
After the rewrite the page parameter stops working on the clean url.
Here's what my rewrite rules look like:
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/?$ file.php?style=$1&redirect=no [NS]

So I translate what you'd like to do then, I change your rules and write them. Please tell me if I'm right or wrong.
You'd like to check if in the query string you have an optional parameter "style".
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
What is the point of the ^ and the /? and the $ ? Remove them:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)
It's clearer this way and should still work for what you want.
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
What is the point of the ? ? Remove it:
RewriteRule ^file\.php$ %1-new [NS,R=301,L]
So, please try these rules and tell me if they work, and if not, please be more specific or give real URL sample that you'd like to rewrite:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)$
RewriteRule ^file\.php$ %1-new [QSA,NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/$ file.php?style=$1&redirect=no [NS]

Related

URL rewrite for a clean format

i am trying to have my urls look like cleaner using the below but its not working i don't know if i should add anything else,
this is how it looks now
example.com/product.php?product_id=144&product_name=50ml-bottle-with-glasses,-shirt
this is how i want them to look
example.com/product/144/50ml-bottle-with-glasses,-shirt
basically here's what i used
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-]+)/?$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests
Thank you
This should work :
RewriteEngine On
#1)redirect from "/product\.php\?product_id=123&product_name=foo" to "/product/123/foo"
RewriteCond %{THE_REQUEST} /product\.php\?product_id=([^&]+)&product_name=([^\s&]+) [NC]
RewriteRule ^ /product/%1/%2? [NE,L,R]
#2)internally map "/product/123/foo" to "/product\.php\?product_id=123&product_name=foo"
RewriteRule ^product/([0-9]+)/(.+)/?$ product.php?product_id=$1&product_name=$2 [B,NC,L]
There are comma into your parameter product_name. Try to change your .htaccess and add comma into your regular expression for this parameter: ([A-Za-z0-9-,]+). So your htaccess will be something like this:
RewriteEngine On
RewriteRule ^product/([0-9]+)/([A-Za-z0-9-,]+)$ product.php?product_id=$1&product_name=$2 [NC,L] # Handle product requests

Rewrite trouble in htaccess

So I have a kind of strange trouble. I have a rewrite rule from
mysite.com/brand.php?brand=example&ref=ex1
to
mysite.com/brand/example/ex1
And I managed to do that, but there is also another problem: when you type this:
mysite.com/brand/example/ex1.html
or
mysite.com/brand/example/ex1.php
I get this:
mysite.com/brand/example/ex1?ref=ex1.php&brand=example
Can anyone tell why I get this redirect and how to get rid of it?
I tried this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)php$ /brand/$1/$2
or this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)\.php$ /brand/$1/$2
but it simply doesn't work.
Please, anyone help me!
I would use the mod_rewrite for this:
RewriteEngine On
RewriteCond %{REQUEST_URI} =/brand.php
RewriteCond %{QUERY_STRING} ^brand=([^&]+&ref=(.+)$
RewriteRule .* /brand/%1/%2? [L,R]
The RewriteRule is applied only when both RewriteCond are valid. The first RewriteCond compares the request URL to be /brand.php, the second matches regular expression against the query string and stores the matches in %1 and %2 respectively.
In the RewriteRule is the URL rewritten using the matched parts from query string.

Why won't my RewriteRules play nice together?

The first part of my code works and redirects/rewrites to new/file. Problem occurs when I try to pass variables. They all get redirected to new/file
Whatever variable I try to pass in file.php?foo=bar redirects to new/file instead of new/file/bar.
for example:
new/file rewrites to file.php
new/file/2 rewrites to file.php?page=2
file.php redirects to new/file
file.php?page=2 doesn't redirect to new/file/2 but get's overwritten to new/file instead
My code:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
Please note that I am passing more than just page variables.
I'm thinking maybe a QSA flag is supposed to go somewhere?
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file [NS,R=301,L]
RewriteRule ^new/file$ file.php?redirect=no [NS]
In line 2 you had ?. This will replace the current querystring, and page=2 is lost.
In line 3 you had a ?. Paths don't contain questionmarks. Secondly the questionmark is interpreted as an "optional letter 'e'", because it is a RegEx. So "/new/fil" would also work (try it out).

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

.htaccess redirect "domain.tld/" to "domain.tld/home.html"

I have a website here on my localhost :
http://localhost/mysite/www/index.php
I have some RewriteRules to redirect like this :
http://localhost/mysite/www/index.php?page=home
-> http://localhost/mysite/www/home.html
And now, I want to do a redirection like this :
http://localhost/mysite/www/
-> http://localhost/mysite/www/home.html
I have an environment variable named REWRITE_BASE containing /mysite/www/. So what I thought to do was to compare {REQUEST_URI} to %{ENV:REWRITE_BASE} ... like this:
RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}
RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]
But it don't works well.
To help you understand what I want to do, here is the working code in PHP to do what I want:
$rewriteBase = getenv('REWRITE_BASE');
if ($rewriteBase === $_SERVER['REQUEST_URI'])
header('Location: '.$rewriteBase.'home.html');
Thanks for help.
Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :
# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]
# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]
# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]
Now it's alright. Thanks for your answers! ;)
What you want to do won't work, because mod_rewrite doesn't expand any variables present in its test patterns. Therefore, when you do something like this...
RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}
...What you're actually doing is comparing the value of %{REQUEST_URI} to the string "%{ENV:REWRITE_BASE}", instead of the value "/mysite/www/" like you wanted. Is there a reason that you can't specify the value directly in your RewriteCond or simply move the rules so that they're relative to the /mysite/www/ directory to begin with?
There might be another way to approach the problem, but unfortunately (and for some good reasons) you cannot perform that kind of comparison using mod_rewrite.

Resources