APACHE Mod Rewrite - Dynamic URLs to Semantic URLs - apache

I have a little web app based on Google Maps. I have URLs like this:
http://www.example.com/web.php?u=olives-restaurante-de-ensaladas-en-margarita
and I want to have exactly this:
http://www.example.com/web/olives-restaurante-de-ensaladas-en-margarita
So I want to convert my parametric dynamic URLs into semantic URLs
How can I achieve this?!
I have tried to put the following code on my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^web/([A-Z0-9]+)/$ web.php?u=$1
But sadly, when I try to access http://www.example.com/web/olives-restaurante-de-ensaladas-en-margarita it redirects me to 404 page.
What I'm doing wrong?

RewriteRule ^web/([A-Z0-9]+)/$ web.php?u=$1
http://www.ubikate.com.ve/web/olives-restaurante-de-ensaladas-en-margarita
Your RewriteRule pattern (^web/([A-Z0-9]+)/$) does not allow hyphens, enforces a trailing slash and only matches uppercase letters, so this won't match the URL you are requesting.
Try something like the following instead:
RewriteRule ^web/([\w-]+)$ web.php?u=$1 [L]
The \w is a shorthand character class that is equivalent to [a-zA-Z0-9_].

Related

301 Redirects Rewrite Rule to replace underscore with hyphen and remove subdirectory

I need help creating a rewrite rule/301 redirect for the following link structures in the .httaccess folder.
Source URL: www.example.com/sub_directory/product_name_1.html
Destination URL: www.example.com/prodcut-name-1.html
The requirements for the redirect then are as follows:
Remove /sub_directory/
Change all underscores '_' to hyphens '-'
Unfortunately my regex isn't very good. I've tried searching around and but the solutions from other post with similar issues where not working for me (such as here)
Any help on a solution for this would be much appreciated. Also if you could please explain the why/how of it. I'd like to be able to better understand this.
Answer from #Walf is close but requires some changes e.g. regex anchors and DPI flag.
You can use these rules on top of your site root .htaccess:
RewriteEngine On
# remove /sub_directory/ when there is no _ left
RewriteRule ^sub_directory/([^_]+)$ /$1 [R=301,NC,NE,L]
# use recursion based rule to replace _ by -
RewriteRule ^(sub_directory/[^_]*)_+(.*)$ $1-$2 [NC,N,DPI]
# rest of your rules go here
Something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(sub_directory/[^_]*)_+(.*) $1-$2 [DPI,N]
RewriteRule ^sub_directory(?:$|/(.*)) http://%{HTTP_HOST}/$1 [R=301,L]
It first loops through URLs that are in that subdirectory, to replace consecutive underscores with a single hyphen. It's done first so it doesn't interfere with other URLs that may contain an underscore. It then externally redirects (the cleaned) requests for that subdirectory to the root. The ugly grouping makes sure it only applies to exactly that folder.

Replacing slashes with dashes in URL Rewriting

How do I accomplish URL rewriting for the following with mod_rewrite in Apache?
Pretty Link : www.mysite.com/pages/category/page/
Actual File : www.mysite.com/html/category-page.html
I only want to rewrite URLs if they contain the domain and the pages directory. In all other cases I want the server to work normally.
I've come up with this but would like to know how to replace slashes with dashes:
RewriteEngine On
#Look for the word "pages" followed by a slash, and then the article title
RewriteRule ^pages/(.+)$ html/$1.html [L]
See if the following rule set works:
RewriteEngine On
RewriteRule ^pages/(.+?)/?$ html/$1.html [N]
RewriteRule ^html/([^/]+)/(.*)$ html/$1-$2 [N]

htaccess rewriting all URLs in a path with character replacement

I have a website that was converted from 1 platform to another. I have many thousands of pages indexed that look like this:
domain.com/test/red_widget.html
domain.com/test/big_red_widget.html
domain.com/test/small_nice_red_widget.html
that need to look like this:
domain.com/test/red-widget.html
domain.com/test/big-red-widget.html
domain.com/test/small-nice-red-widget.html
So all the URLs in question in this example are located in the "test" path, use underscores and have an html extension.
And I need all urls in the "test" path with an html extension to be rewritten as is except the underscores, "_", would be replaced with dashes, "-".
I've tried a lot of stuff but I can get compatibility with my current htaccess rules which need to continue to work.
Right now I have this rule:
RewriteRule ^test/(.*).html$ /?kw=$1
which does work but for a url like domain.com/test/small_nice_red_widget.html, the kw parameter will be set as small_nice_red_widget whereas I really need it to be set as small-nice-red-widget.
Any guidance or if anyone can point me in the right directions will be greatly appreciated.
This should work for you:
RewriteRule ^test/(.*)_(.*)_(.*)_(.*).html$ /test/$1-$2-$3-$4.html [R,L]
RewriteRule ^test/(.*)_(.*)_(.*).html$ /test/$1-$2-$3.html [R,L]
RewriteRule ^test/(.*)_(.*).html$ /test/$1-$2.html [R,L]
Place the above, just above your current rewrite rule.
You can also do this to replace an arbitrary number of underscores with slashes:
RewriteRule ^test/(.*)_(.*)\.html$ /test/$1-$2.html [L,E=DASH:Y]
RewriteCond %{ENV:DASH} Y
RewriteRule ^([^_]+)$ /$1 [L,R=301]

redirecting a dynamic url containing '?' mark

My .htaccess file looks like this.
RewriteEngine On
RewriteRule (.*)-(.*)-(.*) view.php?title=$1&date=$2&author=$3
Using this rule i am able to redirect simple dynamic url's without special characters
domain.com/abcd-June%2027th%202013-authorname
to
domain.com/view.php?title=abcd&date=June%2027th%202013&author=authorname
Similarly, I want to redirect dynamic url's containing question mark '?' in query string
for example
http://domain.com/abcd?-June%2027th%202013-authorname
to
http://domain.com/view.php?title=abcd?&date=June%2027th%202013&author=authorname
How to do this? What are the changes required in .htaccess file?
Use NE flag.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^-]+)-([^-]+)-(.*?)/?\s
RewriteRule ^ view.php?title=%1&date=%2&author=%3 [L,QSA,NE]

Simple mod_rewrite RewriteRule for messy legacy url's

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern
Old URL Examples:
example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription
example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123
example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers
(The old URL's are sometimes hitting 150+ characters.)
New URL's:
example.com/products/category/actual-product-name
There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.
I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:
RewriteRule ^/products.aspx?category=Foo&product=ProductName
/products/category/new-product-name/ [R=301,NC]
But that isn't working. I know this should be simple, but I am stuck. Any ideas?
Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/ [R=301,NC]
Have a look at the RewriteMap directive of mod_rewrite.
You can specify in a text file something like:
products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name
And in your httpd.conf
RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]
Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.
You can try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$
RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.
You have to use RewriteCond to match against the query string.
As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.
For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.