Need to create a RewriteRule in .htaccess for a Magento website - apache

I am currently trying to put a RewriteRule into my .htaccess file for my Magento website that will allow me to write a category URL in the following way:
http://mydomain.com/dir/<direction>/order/<order>/<Magento category URL path>.html
What I am basically looking to do is use my robots.txt file to make some of the category URLs not appear (specifically when you apply a different sort order to the category).
So let's assume I have the following URL:
http://mydomain.com/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
I would like that to be rendered just as if it the URL was:
http://mydomain.com/footwear/mens-work-boots/motorcycle-boots.html?dir=asc&order=sales_index
The code I have put in my .htaccess file is as follows:
RewriteRule ^dir/(.*?)/order/(.*?)/(.*?)$ $3.html?dir=$1&order=$2
For some reason, when I have this in there, I get a 404 error. Can someone point me in the right direction to make this work for me?

I tried with this on my server
RewriteRule ^(.*)/dir/(.*?)/order/(.*?)/(.*?)$ $4.html?dir=$2&order=$3 [R,L]
and when i issue request
http://myserver/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
i get proper redirection to
http://yuave.dev:81/footwear/mens-work-boots/motorcycle-boots.html.html?dir=asc&order=sales_index
May be you are missing [L] flag on your request.

Related

How can i rewrite a image file name to query string via rewrite rules in Apache

I am trying to create a php script that will check if a image is local and if not retrieve it from remote site. I want the user to call the image like this.
http://media.myimage.org/media/2AF97A30-42F7-4E08-8BF6-020316D6F161.jpg
To make it work i need to create a rule that will rewrite the url to the following request
http://media.myimage.org/media/image.php?id=2AF97A30-42F7-4E08-8BF6-020316D6F161
i tried multiple ways and tested them via https://htaccess.madewithlove.be/ but they all fail with
rule was not meet
i want to take my regexpr '/([{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?)(.jpg)/m'
and if it is meet then translate it to 'image.php?id=${1}' if not i want to redirect to a 404 page not found
With some more research I was able to solve my issue. Not sure its the best way but it works. If anyone has a recommendation to improve I am all ears.
RewriteEngine On
RewriteBase /
RewriteRule ^/?([{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?+).(jpg|png|gif)$ /madia/image.php?id=$1&format=$3 [L]
RewriteRule (.*) /images/404.php?image=$1 [L]
The above will accept any image request as long as the image name is a valid guid and the file has the extension of jpg or png or gif. If this Rewrite rule is hit we will exit, if not we will do the 404 rewrite.

Apache 2.4 .htaccess friendly URL rewrite

Having some issues understanding the .htaccess file and getting it to work properly. .htaccess is recognized....i entered plain test at the top and got the internal server error. And it appears mod-rewrite is working...because I downloaded a test php file and it works.
I am using Apache 2.4.23 with no php pages
However i cant seem to get this to work right.
I am trying to take this url
http://example.com/ProjectTest/index.shtml?dynContent=Content1
and rewrite it like this
http://example.com/ProjectTest/Content1
I am also trying to do this generically across the site since ?dynContent=whatever will be a constant.....and trying to keep the same path.....like the below example
Change this
http://example.com/ProjectTest/ProjectFolder/index.shtml?dynContent=Content1
to this
http://example.com/ProjectTest/ProjectFolder/Content1
I am not very good with the .htaccess files
UPDATE: Ok...I got this to work.....not really sure why it works....but when I type in the test-dev/ProjectTest/apples or test-dev/ProjectTest/oranges it calls up the appropriate content.
This is what I used
RewriteRule ^ProjectTest/([^/\.]+)/?$ ProjectTest/index.shtml?dynContent=$1 [L]
and then I rewrote it to be this and now it works for any directory
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^dynContent=(.+)$
RewriteRule ^/(.+)+/index.shtml $1/%1 [PT]
This requires mod_rewrite to be activated. The value of the bracket in dynContent=(.+) is stored in %1 and the bracket's value in ^/(.+)+/index.shtml is stored in $1. Both together creates your target path http://example.com/ProjectTest/ProjectFolder/Content1

Proper mod_rewrite for SEO friendly URLs

I have a custom php script, each having a page for page.php?id=[number] and page.php. I've read a few .htaccess tutorials but I don't know which one is the most appropriate way of doing it:
Example,
page.php?id=12 to page/id-12/and
page.php to page/ AND page
Here's what I currently have:
RewriteEngine On
RewriteRule ^category/([0-9])$ /category.php?id=$1
RewriteRule ^category/$ /category.php
Code above returns 404 not found if i access category but appears okay if i access category/ (with a slash)
All your rules include the slash / at the end of category, thus of course category cannot match it.
RewriteRule ^category/?$ /category.php
The ? after / makes it optional

apache .htaccess - cut a string from url and redirect

For some reason google indexed several pages of my website as:
http://myapp.com/index.php/this-can-be-enything/1234
Now, I want to redirect with apache .htaccess those pages to correct urls:
http://myapp.com/this-can-be-enything/1234
I've googled and tried many options but with no success.
Any tip will be helpful.
I've added to my .htaccess file following lines:
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
I don't know if this is best solution but works ok for me.
Two Parts of problem
To make Google aware that indexed page is moved to some other destination you need to handle that # apache level and issue 301 ( moved permanently )
Handler to handle the cached requested URL to new URL using the #1 handler itself.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.