Issues Replacing underscores to hyphens with .htaccess - .htaccess

Although I already saw few posts about this thema, I am still having some problems to achieve what I want.
My old URL's were like:
http://myhostname.com/offer/1234_Nice_Offer_Cool_Whatever_HEllyeah.htm
And obviously I want to rewrite them to:
http://myhostname.com/offer/1234-nice-offer-cool-whatever-hellyeah.htm
This is the code I tried in .htaccess. This works fine to replace the underscores in URL's which they don't have ANY directory:
RewriteCond %{REQUEST_URI} \.htm$ [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
So, this paire of rules work fine with URL's like:
http://myhostname.com/1234_Some_Section_hellyeah.htm
But when I try the same code for URL's like:
http://myhostname.com/dir1/dir2/dir3/1234_Some_Offer_Or_Section.htm
Then, the server makes an infinite loop (I figure of the [N] flag...)
Basically, I would like to know in which way affects a per-directory URL to these rules and why am I getting this infinite loop. Thanks!

You don't need N flag. To replace underscore by hyphen recursively following code will work:
RewriteRule ^([^_]+)_(.+?\.htm)$ $1-$2 [L,NC,E=underscores:Yes]
RewriteCond %{ENV:REDIRECT_underscores} ^Yes$
RewriteRule ^([^_]+)$ /$1 [R=301,L]

Related

htaccess redirect if parameter exists not working

I'm trying to rewrite any URL if a special parameter exists.
So that this happens:
From: www.example.com/somepage/someother/?entryValue=somevalue
To: www.example.com/somepage/someother/?product=test&special=12345ls&linkSource=website
I tried to following, but it doesnt work as expected:
This code adds var/www/* instead of the link
www.example.com/var/www/web6/htdocs/example.com/index.php/*
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]
This code removes the path:
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ /?product=test&special=12345ls&linkSource=website [L,R]
How can I make it work?
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]
You need to include a slash prefix at the start of the substitution string. Like this:
RewriteRule ^(.*)$ /$1/?product=test&special=12345ls&linkSource=website [L,R]
Without the slash prefix (the URL-path matched by the RewriteRule pattern does not include a slash prefix) it is seen as relative and the directory-prefix (ie. /var/www/...) will be added back and result in the malformed redirect you are seeing.
UPDATE:
but this ends up with "index.php" and the path is lost
You've put the directive in the wrong place and have a conflict. The order of the mod_rewrite directives is important.
Generally, external redirects like this need to go near the top of the .htaccess file, before any internal rewrites (like a front-controller).

Need Assistance for this Htaccess Rewrite Rule

I have a problem with my .htaccess, a short explanation I would like to set http://example.com/newest on my website. However, it always redirects to http://example.com/postname. Where I just need the exact "newest" page. Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^/category/(.*)$ page.php?f=$1
RewriteRule ^/search/(.*)$ search.php?f=$1
RewriteRule ^(.*)/$ post.php?f=$1 <- If this is removed, my post htaccess will not work
RewriteRule ^newest/$ index.php?f=newest <- I want to execute this code
I really don't know what this is called, I have been looking for the whole stackoverflow but I did not get any answer. Please remain me if this is a duplicate question.
As Mohammed implied in comments, your directives are in the wrong order. The line above your "newest" rewrite is a catch-all and rewrites all requests, so the last line will never match.
http://example.com/newest
Note that your rules imply that your URLs should end in a trailing slash. So, you should be linking to http://example.com/newest/ (with a trailing slash), not http://example.com/newest, otherwise your users will get a lot of unnecessary redirects.
However, you appear to be under the belief that the RewriteCond directive applies to all the directives that follow. This is not the case. It only applies to the first RewriteCond directive. You also need some L flags to prevent further processing.
You also have a slash prefix on the "category" and "search" rewrite patterns, so these would never match in a .htaccess context.
Try something like the following instead:
RewriteEngine On
RewriteBase /
# Don't process the request further if it maps to an existing file
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Append trailing if omitted
# Although strictly speaking this only redirects if there are no slashes at all in the URL
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^category/(.*)$ page.php?f=$1 [L]
RewriteRule ^search/(.*)$ search.php?f=$1 [L]
RewriteRule ^newest/$ index.php?f=newest [L]
RewriteRule ^(.*)/$ post.php?f=$1 [L]

RewriteRule to rewrite /folder/subfolder/file to /?f=folder/subfolder/file?

Pretty straightforward - just wondering how do I rewrite this:
/folder/subfolder/file
/?f=folder/subfolder/file
And the number of folders/subfolders might vary, so what I really want is anything after the / in that path (that does not already have ?f= in it) redirected to the path with ?f= added in front of it.
If I add this
RewriteRule ^(.*)$ ?f=$1 [L]
it works just fine but doesn't recognize the existence of ?f= in the URL already, and breaks that existing link with ?f= in there.
Any ideas?
You can use:
RewriteCond %{QUERY_STRING} !(?:^|&)f= [NC]
RewriteRule ^(.*)$ ?f=$1 [L,QSA]
Which does not do the rewriting if the f querystring is already there.
With [QSA] you keep the other possible querystring values.
Try this :
RewriteEngine On
RewriteRule ^(.[^\?]*)$ /?f=$1 [L]

replacing underscore with hyphen in htaccess

I want to change all the underscore in my url to hyphen, presently i am using the below code which is working fine, however i do not want to change the underscore in images link, i just want to change the links to page, is it possible ?
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3-$4 [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3 [R=301,L]
rewriteRule ^([^_]*)_(.*)$ http://example.com/$1-$2 [R=301,L]
please help
Untested code
Add the following statement at the very beginning of RewriteRules block
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|bmp)$ [NC]

htaccess rewrite underscores to dashes for a specific directory

I can successfully rewrite underscores to dashes with the following -- but I need the code to work ONLY for a certain directory, and I can't get that part to work.
WORKS:
RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
DOES NOT WORK:
RewriteRule ^/media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^/media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^/media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
When you remove all leading / from RewriteRule patterns all should work fine (as in the first two example which work).
The the following code:
RewriteRule ^media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
There is also nice online htaccess tester which will help you out verify your configuration e.g.
The tool has some limitations though i.e. currently does not have implemented certain features like %{REQUEST_FILENAME} or %{ENV:...} but for simple rules it should be fine.
I hope that will help.

Resources