Context
I have some urls in my app and some of them used to contain version numbers, ie.
http://myapp.com/some/path/1.2.3
or
http://myapp.com/some/1.2.3
Problem
The application is being rewriten to have - instead of those . in the URLs. For some reason it's impossible to allow . anymore. I would like to create a Rewrite rule that will accept requests in old format and rewrite them to a new one. A version will always have 2 dots.
Stack
PHP.exe running on IIS
EDIT
2 dots!
A version will always have 3 dots.
I guess you meant 2 dots and 3 numbers.
This rule will redirect /1.2.3 to /1_2_3:
RewriteEngine On
RewriteRule ^(.*/\d+)\.(\d+)\.(\d+)/?$ /$1-$2-$3 [L,R=301]
Related
weird one but the referer policy is currently creating issues on my website if the domain has a . on the end, for example:
domain.uk - works fine
domain.uk. - has CORS errors
It seems the . on the end os being treated as part of the domain so considered a different origin. Seems to only be a problem in Chrome. Possibly a Chrome bug?
Thought perhaps I could fix this in my .htaccess by setting up a redirect, but the .htaccess cannot do it as it can only match after the domain, and the . is being treated as part of the domain.
Any suggestions?
It seems the . on the end os being treated as part of the domain so considered a different origin. Seems to only be a problem in Chrome. Possibly a Chrome bug?
It is part of the domain. The trailing dot indicates a fully qualified domain name. If you are only seeing different behaviour in Chrome then maybe Chrome is just being more strict - it's not a bug.
Try https://stackoverflow.com./ (for instance) - you'll probably appear logged out (as the cookies are not passed).
Thought perhaps I could fix this in my .htaccess by setting up a redirect, but the .htaccess cannot do it as it can only match after the domain, and the . is being treated as part of the domain.
You can do it in .htaccess. The dot is sent as part of the Host header (since it is part of the domain) - which is available in the HTTP_HOST server variable. Ordinarily, you'd do this as part of your canonical (www vs non-www / HTTP to HTTPS) redirect, but you could do something like the following using mod_rewrite to remove the trailing dot on the requested hostname:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.+)\.$
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
The %1 backreference contains the hostname, less the trailing dot, that is captured in the preceding condition.
UPDATE:
I am using Wordpress so it already has some rewrite rules in the htaccess, ... can you advise on how to add your rewrites
You need to place this redirect before the existing WordPress directives (ie. before the # BEGIN WordPress section), near the top of the file.
You do not need to repeat the RewriteEngine On directive since that already occurs later in the WordPress section. (If there are multiple RewriteEngine directives then the last instance wins and controls the entire file.)
For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.
I have an old site that is being rebuilt. Instead of using a folders structure, it is using sub-domains. The segments are different, but the redirect itself is pretty simple. I can handle it like so:
RewriteRule ^segment/blog/view$ http://blogs.site.com/segment/article [R=301,NE,NC,L]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
So if I had www.site.com/segment/blog, it will now go to blogs.site.com/segment.
If I had www.site.com/segment/blog/view/catchy_name_goes_here, currently it redirects it to blogs.site.com/segment/article/catchy_name_goes_here and I NEED it to go here: blogs.site.com/segment/article/catchy-name-goes-here.
My issue comes from a decision to change the separator in the URI. The old articles were built with underscores '_' and the new articles are built with hyphens '-'.
How can I replace the underscores in the article titles with hyphens?
Try these rules:
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]
# replace _ by - repeatedly
RewriteRule ^(/segment/blog/view)/([^_]*)_+(.*)$ /$1/$2-$3 [I,N,U]
# all _s gone, now do a redirect
RewriteRule ^/segment/blog/view/([^_]+)$ http://blogs.site.com/segment/article/$1 [R,I,L,U]
I ended up having to use the following. I don't know how many people this might effect due to the unique settings for this site in particular, but thought I would post the answer so it could help anyone that might need it.
The full settings on this are a server running IIS with Server 2k. The site consists of several static content pages, vb script, classic ASP, dot Net, and this is all intertwined with ExpressionEngine pages. It's a mess to say the least. To top it off, Helicon Tech's ASAPI Rewrite Module version 3 is running on the server for .htaccess usage. No sub-expressions, groupings, etc. were taking or being followed/processed. The index.php rule was getting bypassed as well.
This all said, I ended up with the following which parsed everything I needed.
RewriteRule ^index.php/segment/blog/view/([^_]*)_+(.*)$ http://www.site.com/index.php/segment/blog/view/$1-$2 [R,NC]
RewriteRule ^index.php/segment/blog/view/([^_]*)$ http://blogs.site.com/segment/article/$1 [R=301,I,L,U]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]
I have page not found erros in webmaster because the page/2/0 part of a url is due to smart paging module clean url feature (Drupal) now i uninstalled the smart paging module but these page not found errors are still there.
www.mysite.com/a/b/c/page/2/0,
www.mysite.com/a/d/e/page/3/0,
www.mysite.com/a/f/g/page/4/0,
www.mysite.com/a/h/i/page/5/0
and so on.
I want to redirect
www.mysite.com/a/b/c/page/2/0 to www.mysite.com/a/b/c
www.mysite.com/a/d/e/page/3/0, to www.mysite.com/a/d/e
www.mysite.com/a/f/g/page/4/0, to www.mysite.com/a/f/g
www.mysite.com/a/h/i/page/5/0 to www.mysite.com/a/h/i
with one redirect rule. How to do this
in short i want to remove the page/x/0 part from the url and redirect it to the remaining part of that url.
You want something like this:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/[0-9]+/0$ /$1/$2/$3 [L,R=301]
The literals here are after the first 3 path nodes: /anything/anything/anything/, then it must be followed by a page, then some numbers, then a zero. You can make it even more general, say after the "page", by changing the pattern to:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/ /$1/$2/$3 [L,R=301]
I'm converting a site over from Wordpress to a custom CMS and I'm trying to figure out how to handle all of the image paths that are within posts.
The URL structure is http://site.com/wp-content/uploads/2012/09/image-name.jpg
I need to get it in the form of http://site.com/uploads/image-name.jpg
This is what I've got so far:
RewriteRule ^wp-content/uploads/([0-9-])/([0-9-])/([A-Za-z0-9-_]+)/?$ uploads/$3 [R=301,L]
It doesn't seem to be working though, what am I doing wrong?
Almost. You're middle to groupings only match a single digit or -. You need a + after them:
RewriteRule ^wp-content/uploads/([0-9-]+)/([0-9-]+)/([A-Za-z0-9-_.]+)/?$ /uploads/$3 [R=301,L]
And you'll want a slash before uploads/ and you need to include a . as part of the final grouping (to match the extension).