There are a few similar questions on SO, but none that work for this specific scenario.
I want to replace all forward slashes in a URL path with dashes, using mod_rewrite.
So https://stackoverflow.com/foo/bar/baz should redirect to https://stackoverflow.com/foo-bar-baz.
There could be any number of segments in the path (between forward slashes).
I think the solution involves the N flag, but every attempt I've made results in an endless loop.
You can use these 2 rules in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2 [NE,L,R=302]
RewriteRule ^([^/]+)/(.+)$ $1-$2
This will redirect example.com/foo/bar/baz/abc/xyz/123 to example.com/foo-bar-baz-abc-xyz-123
Related
I have a URL like http://example.com/abc+def+cde+ndk
Unfortunately the number of capturing groups in the URI (abc, def,cde..) are not in a fixed number.
I tried writing a rule like the below but it is matching and replacing only three groups(two character groups and one + in between).
RewriteCond %{REQUEST_URI} ^/(.*?)(\+{1,})(.*)$ [NC]
RewriteRule . http://example.com/%1%3 [R=301,L]
Example given below:
Source: example.com/abc+def+cde+x+y(n number of strings separated by +)
Destination Must be: example.com/abccdexy...till n
If you can add a directive to the main config, the best solution is to use a RewriteMap that processes the URL rewriting through an external script, which you write. You can find details on that here.
Basically you do something like:
RewriteMap convertUrl "prg:/www/bin/convertUrl.pl"
RewriteRule \+ ${convertUrl:%{REQUEST_URI}} [R=301,L]
(only the RewriteMap needs to go in your main config, the RewriteRule can go in your .htaccess)
Where /www/bin/convertUrl.pl is a script you write to process the substitution, as described on the above link. It should take the URL on STDIN (without any buffering), strip out the plus signs, and return it on STDOUT.
Something like this should work:
#!/usr/bin/perl
$| = 1; # Turn off I/O buffering
while (<STDIN>) {
s/\+//g; # Replace dashes with underscores
print $_;
}
Here is a pure .htaccess solution.
# Remove a plus sign on each iteration of the rule
RewriteRule ^help/col/([^+/]+)\+([^/]+)$ help/col/$1$2 [E=REMOVED_PLUS_SIGNS:1]
# For URLs that were processed, redirect once all the plus signs are removed
RewriteCond %{ENV:REMOVED_PLUS_SIGNS} =1
RewriteRule ^help/col/([^+/]+)$ /help/col/$1 [R=301,L]
I have such a url:
/keyword1/keyword2/slugged-title-8286-1.jpg?wx=292&hx=164
I would like to forward in this case to:
/images/8286-1.jpg?wx=292&hx=164
the listing number (here 8286) can be 4 or 5 digits and could perhaps contain letters. Also the parameters after ? could be different.
Could you please help me to get this solved?
I haven't done a lot with regex and not sure how this can be done.
You can use this rule in your site root .htaccess:
RewriteEngine On
RewriteRule -(\w+(?:-\d+)?\.jpe?g)$ /images/$1 [L,NE,R=302]
If you don't want a full redirect then use:
RewriteRule -(\w+(?:-\d+)?\.jpe?g)$ /images/$1 [L]
QUERY_STRING is automatically carried over to target URL.
This is my current htaccess
<IfModule mod_rewrite.c>
# turn on rewrite engine
RewriteEngine on
# if request is a directory, make sure it ends with a slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*/[^/]+)$ $1/
# if not rewritten before, AND requested file is wikka.php
# turn request into a query for a default (unspecified) page
RewriteCond %{QUERY_STRING} !wakka=
RewriteCond %{REQUEST_FILENAME} wikka.php
RewriteRule ^(.*)$ wikka.php?wakka= [QSA,L]
# if not rewritten before, AND requested file is a page name
# turn request into a query for that page name for wikka.php
RewriteRule ^(.*)$ wikka.php?wakka=$1 [QSA,L]
</IfModule>
My current url structure is
www.domain.com/site/pool/Page_Example_Test
www.domain.com/site/pool/Page_Example_Test/edit
www.domain.com/site/pool/Page_Example_Test/edit?id=1
www.domain.com/site/pool/Page_Example_Test/history
www.domain.com/site/pool/Page_Number_Room
www.domain.com/site/pool/Page_Number_Room/edit
How Is it possible to access them like this
www.domain.com/site/pool/Page/Example/Test
www.domain.com/site/pool/Page/Example/Test/edit
www.domain.com/site/pool/Page/Example/Test/edit?id=1
www.domain.com/site/pool/Page/Example/Test/history
www.domain.com/site/pool/Page/Number/Room
www.domain.com/site/pool/Page/Number/Room/edit
having the htaccess change only those "/" for "_"
There is only /history and /edit finishing the url name nothing more, the normal is without /edit or /history.
If there are between one and five pieces in the page name, such as
Page
Page/Page
Page/Page/Page
Page/Page/Page/Page
Page/Page/Page/Page/Page
and every element starts with a capital letter and every other part of the url is only lowercase, then you can accomplish this with four replacements (the one-piece-only one needs no replacement). For example, for three pieces:
Find what: ([A-Z][a-z]+)\/([A-Z][a-z]+)\/([A-Z][a-z]+)(\/)?
Replace with: $1_$2_$3$4
You can try it here (although I don't understand why it's only replacing with spaces in every line but the last).
Notes:
Each part (...) is captured
The final slash \/? is optional, and is the slash before the potential "edit" or "history".
In some regex flavors, you don't need to escape the slash /, but it's safer to do so: \/.
Each $[number] is a capture group reference
WARNING! These replacements must be done from longest to shortest: five pieces, then four, then three, then two. Otherwise, you'll seriously mess things up.
All the links in this answer come from the Stack Overflow Regular Expressions FAQ. Please consider bookmarking it for future reference. In particular, see the list of online regex testers in the bottom section, so you can try things out yourself.
Place this code in /site/pool/.htaccess:
RewriteEngine On
RewriteBase /site/pool/
RewriteRule "^(Page)/([^/]+)/([^/]+/.*)$" /$1/$2_$3 [L]
RewriteRule "^(Page)/([^/]+)/([^/]+)$" /$1/$2_$3 [L]
i have a problem with duplicate pages for SEO on a website i'm trying to fix. www.example.com/category/c1234 loads just the same as www.example.com/category/c1234garbage
I've been reading online and testing the code and so far I narrowed it down to a possible regex problem. I have the following lines
# url rewrites
RewriteCond %{REQUEST_URI} ^/index\.cfm/.+ [NC]
RewriteRule ^/index.cfm/(([^/]+)/?([^/]+)?)/?(.*)? /index.cfm/$4?$2=$3 [NS,NC,QSA,N,E=SESDONE:true]
I added an R in the rule so I could see if it was passing through there and it is and after it passes that the garbage at the end disappears.
Can someone help me understand this and figure out a way to fix it so when you go to www.example.com/category/c1234garbage it redirects to www.example.com/category/c1234
I've been searching online for quite a while now and thought it might be time to post here since I can't seem to find a solution. I'm reading "Mastering Regular Expressions" but it might take take a while for me to find the answers I'm looking for.
I appreciate any help you can give me. Thank you.
EDIT: This is what i have before that
RewriteEngine On
Rewritebase /
# remove trailing index.cfm
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index.cfm(\?)?$ / [R=301,L]
# remove trailing slash
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Remove trailing ?
RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule ^/?(index\.cfm)? /? [R=301,L]
# SEF URLs
SetEnv SEF_REQUEST false
RewriteRule ^[a-z\d\-]+/[a-z]\d+/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
RequestHeader add SEF-Request %{SEF_REQUEST}e
RewriteCond %{HTTP:SEF_REQUES} ^true$ [NC]
RewriteRule . - [L]
EDIT: I was reading the htaccess again and found this that I don't understand but it might have some connection. It's located at the bottom of the file.
# lowercase the hostname, and set the TLD name to an enviroment variable
RewriteCond ${lowercase:%{SERVER_NAME}|NONE} ^(.+)$
RewriteCond %1 ^[a-z0-9.-]*?[.]{0,1}([a-z0-9-]*?\.[a-z.]{2,6})$
RewriteRule .? - [E=TLDName:%1]
From your description and your code, it sounds like this is the transformation that's happening here:
www.example.com/category/c1234garbage
↓
www.example.com/index.cfm?category=c1234garbage
So the problem, I think, is not your rewriting rules. The problem is how you're handling querystring parameters on the server side. If you have an actual page called index.cfm that's interpreting those parameters, you should tweak the code behind that page to validate them and redirect to /category/c1234 where appropriate.
I think the code in index.cfm is looking at the parameter, checking to see if it starts with something recognizable, and going from there. You need to make it more strict.
Alternatively, you could add another .htaccess rule to parse the c1234garbage part and decide which part is valid, and which part (if any) is garbage. I can't give you a regex for that, though, since I don't know the rules for a valid input in your application.
Edit:
I think I found the problem. This part here:
RewriteRule ^[a-z\d\-]+/[a-z]\d+/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
You specify the beginning of the relative URL with ^, but you don't specify that you want it to match all the way to the end. So I think what's happening is that it's taking the part of the string that matches, throwing out everything else, and appending it to /index.cfm/. So it takes only the /category/c1234 part from /category/c1234garbage, because that's the part that matches ^[a-z\d\-]+/[a-z]\d+/?.
You can probably fix this with just a word break:
RewriteRule ^[a-z\d\-]+/[a-z]\d+\b/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
If that doesn't work, I'm afraid we've reached the end of my htaccess knowledge. I'm more of a regex guy.
Just BTW, this still seems a little awkward. If I understand this right, part of the URL will still get thrown out if it doeesn't fit your exact pattern. E.g. /category/c1234?abc=123 will lose its querystring parameters. You might want to redesign how your rules are set up.
I partially solved the problem. I added
# Remove garbage from after category
RewriteCond %{REQUEST_URI} [a-z\d\-]+/[a-z]\d+(.+)
RewriteRule ^([a-z\d\-]+/[a-z]\d+)/? $1 [R=301]
on top of the SEF rules. It's doing what i want which is to remove the garbage from the url but it gives me an infinite loop because its redirecting even when the url is clean. Any hints?
EDIT: So i realized that the .+ at the end is matching the numbers as well... How do i change it to match anything other than numbers after the numbers? basically where I have the .+ i need to have a "match any character except for numbers"
EDIT: I finally got it to work with the following code:
# Remove garbage from after category
RewriteCond %{REQUEST_URI} [a-z\d\-]+/[a-z]\d+[A-Za-z-.]+
RewriteRule ^([a-z\d\-]+/[a-z]\d+)/? $1 [R=301]
The (.+) i was using previously was reading the 2nd number (c1234)as being part of the . so it would always pass the the condition as true unless it was something like c1
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^haveg/employer/([0-9]+)/(.*) haveg/employer.php?msg_id=$1
It works fine when i use
http://localhost/haveg/employer/7003/the-message-title
or
http://localhost/haveg/employer/7003/
The problem is here http://localhost/haveg/employer/7003 because i removed the forward slash at the end. it gives page not found error.
RewriteRule ^haveg/employer/([0-9]+)/?(.*) haveg/employer.php?msg_id=$1
I think adding a questionmark should allow it to match.
Try changing your last line to
RewriteRule ^haveg/employer/([0-9]+)([^0-9]*) haveg/employer.php?msg_id=$1
This should accept both cases.
I suggest you add another RewriteRule to make your intention clear. In this code the first rule handles the case when the URL ending with digits followed by an optional slash (when the msg_id query field is blank), and the second applies where there is a message following the digits.
RewriteRule ^haveg/employer/([0-9]+)/?$ haveg/employer.php?msg_id=
RewriteRule ^haveg/employer/([0-9]+)/([^/]+)$ haveg/employer.php?msg_id=$1 [L]