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]
Related
I tried using some code I found elsewhere on stackoverflow, but I want to modify it, and I don't know how.
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^(.+)$ /$1 [R=301,L]
This changes all underscores to hyphens. However, I need to limit this set of statements to only URLs that begin:
http://www.mydomain.tld/article...
In other words, the rules should only apply when the file name begins with the word article.
Can someone help?
Try this and see if this and see if it works for you..
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^article(.+)$ /article$1 [R=301,L]
This is not as straight forward as the title might imply. I'll try to explain.
I'm currently working on a video website based on rewritten urls.
I'm using this rule currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
This is used to let users access videos with good looking urls like this:
domain.com/kJbSGe5X instead of domain.com/?v=kJbSGe5X or domain.com/index.php?v=kJbSGe5X for example.
Now the problem is that whenever a trailing slash is added, the css breaks.
I've tried solutions like adding a slash in front of the css url, like this:
<link href="/css/bootstrap.css" rel="stylesheet">
... but it's not working.
Could a solution be to rewrite all urlstrings after a trailing slash (including the trailing slash) to the same url, without the trailing string? Like this:
domain.com/kJbSGe5X/ or domain.com/kJbSGe5X/randomchars to this:
domain.com/kJbSGe5X - and how would I go about doing so?
I guess there probably is much better solutions to this problem, but I'm rather new at this.
Any help is appreciated.
Thanks in advance!
--EDIT--
I would prefer a solution where everything after a trailing slash gets redirected to the same url without the trailing slash + any string after the trailing. (If there is no content in said url)
I might have put to much emphasis on the css issue - A rule like this would work great with how my website is setup.
Insert this rule before your existing rule to remove any trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^/]+)/.*?\sHTTP/ [NC]
RewriteRule ^ /%1? [R=301,L]
You could try ignoring the css directory in your rewrite rule:
RewriteCond %{REQUEST_URI} !^/css/
This isn't exactly what you asked for but if you excluded specific directories from your rewrite rule (probably /css, /js and so on) then you would not have to worry about formatting your nice/short view URLs to remove anything after the slash or whatever else.
Here is my complete solution. With a little help from anubhava!
Remove trailing slash from all urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1? [R=301,L]
Constrain the "create goodlooking" urls thingy to only work when it's 8 characters (which is the length of each shortlink for videos):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{8})$ index.php?url=$1 [QSA,L]
has the following redirect rule
RewriteRule ^(.*).html(.*)$ manager.php [L]
but I wish that it was not perform to the next file
somefile.html
It's kinda vague exactly what you're looking for but if you want to redirect every URL that ends in .html to manager.php except somefile.html use
RewriteCond %{REQUEST_URI} !somefile\.html$ [NC]
RewriteRule ^.*\.html$ manager.php [NC,L]
Note, that the dot . before html should be escaped and that there's no need to capture the file name or path (using ()). If you're actually expecting URLs that could have .html in the middle use
RewriteRule ^.*\.html.*$ manager.php [NC,L]
Add a condition before it to exclude it:
RewriteCond %{REQUEST_URI} !somefile\.html
RewriteRule ^(.*).html(.*)$ manager.php [L]
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]
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.