I have the following rules in my htaccess file:
RewriteRule ^client-hub/roundtable/([0-9]+)$ /client-hub/roundtable?roundtable=$1 [QSA,L]
RewriteRule ^client-hub/roundtable/([0-9]+)/setup$ /client-hub/roundtable/setup?roundtable=$1 [QSA,L]
The first rule works fine, the second does not.
An expected input would be /client-hub/roundtable/123/setup
and the expected end url would be:
/client-hub/roundtable/setup?roundtable=123 (which works fine if accessed directly), but this second rule doesn't appear to work.
Any suggestions?
Related
Currently I have below code in the htaccess
RewriteRule ^([en|ar]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
Its working fine if the url is http://test.localhost/en/home/
But its not working if the url is http://test.localhost/en/category/1/
I tried the below code but its not working
RewriteRule ^([en|ar]{2})/(.*)/(.*)$ $3?lang=$1&%{QUERY_STRING}&%{QUERY_STRING} [L,QSA]
Your pattern ([en|ar]{2}) is not correct as characters are not grouped inside [...]. In other words it can match ee, er, ||, aa also which is not what you want.
Change your rule to this:
RewriteRule ^(en|ar)/(.*)$ $2?lang=$1 [L,QSA]
And make sure there are no other conflicting rewrite rules. In case this doesn't work I suggest you provide your full .htaccess in question.
Below is my code for .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /products/product-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /buy/buy-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2
First rule is working fine but its not taking the second rule...Unable to understand what is happening here...
Original URL's are like this
www.example.com/products/product-full-view.php?src=somevalue&id=somevalue
and for second one
www.example.com/buy/buy-full-view.php?src=somevalue&id=somevalue
Please help me to run second rule also.
Thanks in advance
You're trying to match the same pattern, give or take an optional trailing slash, four times. By the time you reach the last two rules, your URL is already rewritten to something else.
You probably want something that looks more like:
RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ /products/product-full-view.php?id=$1 [L]
RewriteRule ^buy/([0-9]+)/?$ /buy/buy-full-view.php?id=$1 [L]
Or:
RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2 [L]
RewriteRule ^buy/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2 [L]
Or something to that order anyway.
Note the stub and the [L] each time: the products/ and buy/ avoid that the same URL refers to two different locations, and the [L] (for "Last") tells the rewrite engine to stop processing rules when it gets matched.
If a URL matches the second rule, it also matches the first rule. After applying the first rule, the resulting URL no longer matches the second rule. That's why the second rule is never applied.
Have recently been making use off the htaccess file to create rewrites.
Everything works well, but have made a recent change to include a category name followed by the product, like this:
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
It just used to be:
http://localhost/products/Antalya_Blanc.html
When everything worked ok. The rewrite rules where as follows:
RewriteRule ^products/(.*).html$ product_details.php?&furl=$1 [NC,L]
RewriteRule ^products/images/(.*)$ images/$1
RewriteRule ^products/picture_upload/(.*)$ picture_upload/$1
RewriteRule ^products/thumbnail.php(.*)$ thumbnail.php$1
So to accommodate the new change I have changed the rewrite rules as follows:
RewriteRule ^(.*)/products/(.*).html$ product_details.php?&furl=$1 [NC,L]
RewriteRule ^(.*)/products/images/(.*)$ images/$1
RewriteRule ^(.*)/products/picture_upload/(.*)$ picture_upload/$1
RewriteRule ^(.*)/products/thumbnail.php(.*)$ thumbnail.php$1
Now this kind of works because
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
Will load, but none of the images do - which does not make sense to me when the page does? Why don't the images?
If I change the rewrite rule to:
RewriteRule ^Limestone_Tiles/products/images/(.*)$ images/$1
The images will load for that particular category!
If I put the image url into the browser:
http://localhost/Limestone_Tiles/products/images/face.png
I get the follow message:
The requested URL /images/Limestone_Tiles was not found on this server.
Which is also odd as /images/Limestone_Tiles isn't the Url I have just pasted into the address bar.
Perhaps my rewrite rule is incorrect still?
Many thanks for your help in advance.
This is because your rules have groupings that backreference the first grouped match and not the match you really want. Say, given this rule:
RewriteRule ^(.*)/products/(.*).html$ product_details.php?&furl=$1 [NC,L]
And this URL:
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
The rewritten URI will be: /product_details.php?&furl=Limestone_Tiles
Whereas before, with the older rule, it would have been rewritten to: /product_details.php?&furl=Antalya_Blanc
If that's your intended behavior, then that's fine, except it's breaking the image rule:
RewriteRule ^(.*)/products/images/(.*)$ images/$1
The $1 backreferences the first grouped match (.*), the one before /products/images/, and not the actual image. It needs to be changed to:
RewriteRule ^(.*)/products/images/(.*)$ images/$2
You're probably better off changing the HTML content and fixing this image stuff there as opposed to creating a rewrite rule to try to correct it. You could try adding this to the header of your pages:
<base href="/">
Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.
I am using .htaccess file to redirect my archived old aspx urls to my new php site. Now the requirement is as follows:
example.com/showad.aspx?adid=1234 should be example.com/ad/getad/1234
I googled the matter and got the following htaccess rule:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1
The rule works except that the resulting url is example.com/ad/getad/1234?adid=1234
I searched again and learned that to remove the (adid=1234) at the end of the output url you should place a question mark ? at the end of the substitution parameter, so that the rule should be:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1?
But it doesn't work!
The resulting url is STILL having the original query string appended to it. I am using this site to test my htaccess rules: htaccess.madewithlove.be
Any clues why is this happening?
Try changing your rule to
RewriteRule ^showad\.aspx$ /ad/getad/%1? [R=302,L]
Your rule is fine - I just tested it in my own .htaccess. It's the htaccess tester website that's wrong :)